Python實現多屬性排序 - ThisYanOK - 博客園
https://www.cnblogs.com/thisyan/p/9867372.html
Cspiration 留學兩年多刷題過三千,教你刷題不再困難;全美唯一Java版本Leetcode視頻講解
https://cspiration.com/leetcodeClassification#10309
Python endswith() 函數 判斷字符串結尾_Linux編程_Linux公社-Linux系統門戶網站
https://www.linuxidc.com/Linux/2015-02/112972.htm
python中定義結構體的方法_Python_lxc521wjh的博客-CSDN博客
https://blog.csdn.net/lxc521wjh/article/details/53224563
code_merge.py
# -*- coding: cp936 -*-
import os
import sys
import re
# 遞歸獲取路徑下所有文件名
def listdir(path, list_name): #傳入存儲的list
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
else:
list_name.append(file_path)
def file_name(file_dir):
file_type=['.c','.cc','.cpp','.sh','.py','.java','.bat','.h','.hpp','config','.gn','.md','.mk']
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
flag=False
#print(file)
type_i=os.path.splitext(file)[1]
#print(type_i)
if type_i in file_type:
flag=True
if 'code_merge' in file:
flag=False
if flag: #or os.path.splitext(file)[1] == '.jpeg':
L.append(os.path.join(root, file))
return L
#其中os.path.splitext()函數將路徑拆分爲文件名+擴展名
if not os.path.exists('./copy_list.txt'):
file_list=open('./copy_list.txt','w')
L=file_name(os.getcwd())
for i in L:
file_list.write(i+"\n")
file_list.close()
#os.system('python code_extract.py copy_list.txt')
#extract_file=sys.argv[1]
extract_file="copy_list.txt"
print(extract_file)
out_file=open('bluetooth_merge.txt','w')
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
if len(strn)>2 and os.path.isfile(strn.strip()):
#print(strn)
strn=strn.strip()
temp_file=open(strn)
cnt=0
out_file.write(strn+"\n")
for i in temp_file.readlines():
cnt=cnt+1
out_file.write(i)
out_file.write("\n")
out_file.close()
使用python字符串處理的的原則:
- 每個字符串最好strip()一下,str.strip()
- 使用正則表達式最好判斷一下
m=re.search(正則表達式,字符串)
if m:
str1=m.group()
3.
先創建一個腳本得到當前文件夾下所有的文件:
find . -name "*.*">copy_list.txt
python code_extract.py copy_list.txt
#cat copy_list.txt | while read line
#do
#echo "$line"
#egrep "(?:[a-zA-Z_]\w*)\ (?:[a-zA-Z_]\w*)\ (?:[a-zA-Z_]\w*)" "$line">>result.txt
#python
#done
rm -f copy_list.txt
然後寫"code_extract.py",如下:
import os
import sys
import re
extract_file=sys.argv[1]
print(extract_file)
out_file=open('bluetooth_out.txt','w')
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
if len(strn)>2:
#print(strn)
strn=strn.strip()
temp_file=open(strn)
cnt=0
#out_file.write(strn+"\n")
for i in temp_file.readlines():
cnt=cnt+1
m=re.search(r'(?:[a-zA-Z_]\w*)\ (?:[a-zA-Z_]\w*)\ (?:[a-zA-Z_]\w*)\(.*\)',i)
if m:
out_file.write(m.group()+":"+str(cnt)+"\n")
#out_file.write("\n")
out_file.close()
https://github.com/YuhanLiin/Lexer-and-Parser-Python/blob/master/Lexer.py
https://github.com/almoskowitz/StringProcess/blob/master/Tweets.py
https://github.com/priyaraghu/StringProcessing 可以參考一下2個文件的比較
https://github.com/BiaChaudhry/stringProcessorModulePython 可參考性
https://github.com/PollyP/StringsFromGdbProcessMappings 與gdb有關,可能有用
https://github.com/JavaMousekin/strings_process 可能有用
https://github.com/nayamama/String-Processing/tree/master/suffix%20tree 十分有用的,語法樹與trie樹
https://github.com/kowai-monsuta/string-processing 可能有用
https://github.com/igorsergiichuk/string_processor 可能有用
https://github.com/brarajit18/String-processing/blob/master/string_methods.py 要使用numpy,一般電腦難以使用
https://github.com/Kimaya27/Processing-Strings/blob/master/Processing%20Strings.py
https://github.com/adidasv111/String-Generation-and-Processing 字符串生成
https://github.com/tomjpsun/oosheet/blob/master/android_to_ios.py python處理表格
https://github.com/YusraKhalid/String-Manipulation 實用的字符串處理函數
https://github.com/PlamenAngelov/Python-Fundamentals-05-Strings_and_Text_Processing
https://github.com/Nduhiu17/message-processor
https://github.com/ihfazhillah/string_processing_algorithms_in_c_and_python 必看部分
https://github.com/gppeixoto/pcc
https://github.com/sstajinder1/Witty-Jumbler
https://github.com/mmxa/MathematicalModelingCompetition 比較有意思的xlms,excel表格
https://github.com/AwdeTeam/pytextdiff
https://github.com/tornadoHunter/six_houses_game 小遊戲
https://github.com/mazerlodge/JeffersonKeyCode/blob/master/JeffersonKeyCode_DictionaryVersion.py
https://github.com/bluekitchen/btstack/tree/master/src 簡易的btstack代碼
https://github.com/bluekitchen/bluetooth-logger/tree/master/lib 藍牙logger
https://github.com/corakwue/ftrace/tree/master/ftrace 比較好的parser
https://github.com/zmdyemwp/Android_Log_Parser 重點看的
https://github.com/vectorxiang/nfcAnalysis/blob/master/nfcAnalysis.cpp
https://github.com/EricChows/Android-log-system
https://github.com/qijisui/log_analysis/blob/master/loga.py 比較好的bt stack log分析,雖熱是android的
https://github.com/mikeryan/PyBT
https://github.com/JiounDai/Bluedroid
https://github.com/abing1991/bt
通過以下命令,可以得到一個目錄下的所有文件名字:
find . -name "*.*" > copy_list.txt
然後使用以下腳本,合併一個類型的文件;
通過從控制檯作爲輸入,提取希望提取的.後綴文件suffix,如下:例如
python bluetooth_merge.py mk_copy_list.txt
希望提取mk文件
import os
import sys
#extract_file=r"./copy_list.txt"
extract_file=sys.argv[1]
print(extract_file)
#file_type=['.c','.h','.cc','.java','.cpp','.py','.hpp','.sh']
file_type=[]
temp_strlen=len(extract_file)-len("_copy_list.txt")
file_type.append(extract_file[0:temp_strlen])
file_type[0]=r"."+file_type[0]
out_file=r'bluetooth_out.txt'
fo_array=[]
fo_flag=[0]*len(file_type)
index=0
for i in file_type:
fo=open(str(index)+i+out_file,'w')
fo_array.append(fo)
index=index+1
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
#print(strn+'\n')
flag=False
strn1=strn.strip()
index=0
for extension in file_type:
tempflag=strn1.endswith(extension)
#flag=flag or tempflag
#print(str(tempflag)+'\n')
if tempflag:
print(strn)
fo_array[index].write('\n')
fo_array[index].write(strn)
temp_file=open(strn1)
fo_array[index].write(temp_file.read())
fo_array[index].write('\n')
temp_file.close()
else:
print('not expected file type \n')
index=index+1
else:
print("no "+extract_file+" excute bluetooth_extract.sh \n")
os.system("sh bluetooth_extract.sh")
for i in fo_array:
i.close()
首先使用一下shell腳本,得到文件txt,腳本命名爲bluetooth_extract.sh
find . -name "*.*" -type f |xargs egrep "bluetooth|bluez|bluedroid|bt" >copy_list.txt
python bluetooth_merge.py
然後使用bluetooth_merge.py腳本出來copu_list.txt,並通過不同的後綴名.c,.cpp等將不同後綴文件,合併爲一個代碼文件;
該語句判斷後綴, tempflag=strn1.endswith(extension)
import os
extract_file=r"./copy_list.txt"
file_type=['.c','.h','.cc','.java','.cpp','.py','.hpp','.sh']
out_file=r'bluetooth_out.txt'
fo_array=[]
fo_flag=[0]*len(file_type)
index=0
for i in file_type:
fo=open(str(index)+i+out_file,'w')
fo_array.append(fo)
index=index+1
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
#print(strn+'\n')
flag=False
strn1=strn.strip()
index=0
for extension in file_type:
tempflag=strn1.endswith(extension)
#flag=flag or tempflag
#print(str(tempflag)+'\n')
if tempflag:
print(strn)
fo_array[index].write('\n')
fo_array[index].write(strn)
temp_file=open(strn1)
fo_array[index].write(temp_file.read())
fo_array[index].write('\n')
temp_file.close()
else:
print('not expected file type \n')
index=index+1
else:
print("no "+extract_file+" excute bluetooth_extract.sh \n")
os.system("sh bluetooth_extract.sh")
for i in fo_array:
i.close()
將一個txt文本中的換行文本,全部合成一段文本,即去除換行符號,如下:
str.strip()去除字符串的'\n'換行符號
#coding=utf-8
def clearBlankLine():
file1 = open('before_blank.txt', 'r') # 要去掉空行的文件
file2 = open('after_blank.txt', 'w') # 生成沒有空行的文件
try:
for line in file1.readlines():
if line == '\n':
print("blank line \n")
else:
file2.write(line.strip())
finally:
file1.close()
file2.close()
if __name__ == '__main__':
clearBlankLine()
將一個txt文本中的空白行去除,如下:換行符號並不一定是'\n',可能一個空白行,有多個字符'\n'與其他字符混合的情況,
根據字符串長度判斷,len(line)<4情況,去除空白符號,可能不準確。
#coding=utf-8
def clearBlankLine():
file1 = open('before_blank.txt', 'r') # 要去掉空行的文件
file2 = open('after_blank.txt', 'w') # 生成沒有空行的文件
try:
for line in file1.readlines():
#print(len(line))
#print('\n')
if len(line)<4:
print("blank line \n")
else:
file2.write(line)
#file2.write('\n')
finally:
file1.close()
file2.close()
if __name__ == '__main__':
clearBlankLine()
將一個txt文本去重複;txt文本去重複
方法1:使用set集合,去除重複的,有的表面看着一樣,實際不一樣,比如,"efe"與"efe ",有一個空格差別
import shutil
readPath='repeat.txt'
writePath='after_repeat.txt'
lines_seen=set()
outfiile=open(writePath,'w')
f=open(readPath,'r')
for line in f:
if line not in lines_seen:
lines_seen.add(line)
#outfiile.write(line)
for line in lines_seen:
outfiile.write(line)
方法2:自己寫的方法,維護一個List
res_list = []
f = open("repeat.txt",'r')
dup=[]
index=0
dul=open("repeat1.txt",'w')
for line in f.readlines():
index=index+1
if line in res_list:
print('in list')
else:
line.replace("\n"," ")
dul.write(line)
res_list.append(line)
判斷一個c文件合集中的頭文件,去除重複後的頭文件有哪些:還是用到集合set的思想,搜索"include"字符串
import re
import shutil
read_list = []
f = open("0.cbluetooth_out.txt",'r')
dup=[]
lines_seen=set()
#index=0
dul=open("c_include.txt",'w')
for line in f.readlines():
#index=index+1
if re.search(r'include',line):
if line not in lines_seen:
lines_seen.add(line)
dul.write(line)
else:
print("no include \n")
對android源代碼文件夾中的文件,提取後綴名;先通過find . -name "*.*" > houzui.txt得到文件:然後提取後綴:使用例子,python houzui.py houzui.txt 6
import os
import sys
import shutil
#extract_file=r"./copy_list.txt"
extract_file=sys.argv[1]
split_num=int(sys.argv[2])
print(extract_file)
file_type=set()
out_file=r'houzui_out.txt'
index=0
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
strn1=strn.strip()
strn2=strn1[-10:-1]+strn1[-1]
suffix_arr=strn2.split(".")
suffix="."+suffix_arr[-1]
if (suffix not in file_type) and (len(suffix)<5):
file_type.add(suffix)
else:
print("no "+extract_file+" \n")
print("suffix num: "+str(len(file_type))+"\n")
f=open(out_file,'w')
counter=0
for i in file_type:
if counter==split_num:
f.write("\n")
counter=0
f.write(i)
f.write("\t")
else:
f.write(i)
f.write("\t")
counter=counter+1
f.write("\n")
f.close()
由上面的文件,若修改後,需要根據空格讀取字符串,然後重新排序,如何做?如下:python houzui_blank.py a_houzui_out.txt 6,
其中比較重要的是字符替換,將\t製表符,換爲" "空格,這樣可以split
import os
import sys
import shutil
#extract_file=r"./copy_list.txt"
extract_file=sys.argv[1]
split_num=int(sys.argv[2])
print(extract_file)
file_type=set()
out_file=r'houzui_blank_out.txt'
index=0
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
strn1=strn.strip()
strn1=strn1.replace("\t"," ")
suffix_arr=strn1.split(" ")
print(suffix_arr)
for suffix in suffix_arr:
if (suffix not in file_type):
file_type.add(suffix)
else:
print("no "+extract_file+" \n")
print("suffix num: "+str(len(file_type))+"\n")
f=open(out_file,'w')
counter=0
for i in file_type:
if counter==split_num:
f.write("\n")
counter=0
f.write(i)
f.write("\t")
else:
f.write(i)
f.write("\t")
counter=counter+1
f.write("\n")
f.close()
根據一個txt,提取txt中每行包含一個string字符串,並寫入到新的文件中;python extract.py 0.cbluetooth_out.txt _sdp_cback 0
其中比較簡單的判斷字符串1是否在字符串2中
import os
import sys
import shutil
#extract_file=r"./copy_list.txt"
extract_file=sys.argv[1]
extract_str=sys.argv[2]
split_num=int(sys.argv[3])
print(extract_file)
file_type=set()
out_file=r'extract_out.txt'
index=0
if os.path.exists(extract_file):
f=open(extract_file,'r')
for strn in f.readlines():
strn1=strn.strip()
if extract_str in strn1:
if (strn1 not in file_type):
file_type.add(strn1)
else:
print("no "+extract_file+" \n")
f=open(out_file,'w')
counter=0
for i in file_type:
if counter==split_num:
f.write("\n")
counter=0
f.write(i)
f.write("\t")
else:
f.write(i)
f.write("\t")
counter=counter+1
f.write("\n")
f.close()
對提取內容排序,是shell: cat extract_out.txt |sort > 1.txt
過濾一個文件中的內容,根據後綴名字過濾;
egrep "\.c|\.cpp|\.h|\.java|\.hpp|\.py|\.mk" 1.txt > copy_list.txt
對於android_9版本:需要關注的代碼:
./frameworks/base/core/java/android/bluetooth/BluetoothA2dp.java
./frameworks/base/core/java/android/bluetooth/BluetoothA2dpSink.java
./packages/apps/Bluetooth/src/com/android/bluetooth/a2dp/*.java
./packages/apps/Bluetooth/src/com/android/bluetooth/a2dpsink/*.java
./packages/apps/Bluetooth/src/com/android/bluetooth/avrcp/*.java
./packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/*.java
./packages/apps/Bluetooth/src/com/android/bluetooth/sdp/*.java
刪除重複行:
cat android_4.4.4_r1/*.txt android_9/*.txt>1.txt
sort -n 1.txt |uniq
cat執行腳本:在a2dp.txt中放入android_9內容
for line in `cat a2dp.txt`
do
echo $line >>result.txt
echo >>result.txt
cat $line>>result.txt
echo >>result.txt
done