Ⅰ python 文本內容提取
#!/usr/bin/python3
#-*-coding:utf-8-*-
defparse(text):
result=[]
importre
r1=re.compile(r's*(/[^s]+)s+FaceTrackings+{([^}]*)}s+(([^)]*))')
r2=re.compile(r's*FD_Faces+(([^)]*))')
pos=0
whileTrue:
m=r1.match(text[pos:])
ifnotm:
break
data={}
data['source']=m.group(1)
keys=m.group(2).split(',')
values=m.group(3).split(',')
attrs=dict(map(lambdax,y:[x,y],keys,values))
data.update(attrs)
pos+=m.end()
face=[]
forxinrange(int(data['FaceNumber'])):
m=r2.match(text[pos:])
ifnotm:
break
face.append(m.group(1).split(','))
pos+=m.end()
data['FD_Face']=face
result.append(data)
returnresult
defmain(input_file,output_file):
f=open(input_file,'r')
text=f.read()
f.close()
result=parse(text)
buff=[]
fordatainresult:
buff.append('miFileIndex:{miFileIndex}'.format(**data))
buff.append('source:{source}'.format(**data))
buff.append('FaceNumber:{FaceNumber}'.format(**data))
i=0
forfaceindata['FD_Face']:
i+=1
buff.append('Face{0}:({1})'.format(i,','.join(face)))
buff.append('')
f=open(output_file,'w')
f.write(" ".join(buff))
f.flush()
f.close()
if__name__=='__main__':
importsys
iflen(sys.argv)==3:
main(sys.argv[1],sys.argv[2])
Ⅱ 怎麼用python提取一段文本中的數字
import re
s = 'MA5:12.56 MA10:12.69 MA20:12.19'
print(re.findall(r'\d+\.\d+', s))
# ['12.56', '12.69', '12.19']
Ⅲ 使用python對txt文本進行分析和提取
實現的方法和詳細的操作步驟如下:
1、首先,打開計算機上的pycharm編輯器,如下圖所示,然後進入下一步。
Ⅳ Python如何批量提取txt文件中的指定位置的數字
你好,我是大魚兒不吐泡泡,用網路網盤分享給你,點開就可以保存,鏈接永久有效^_^鏈接:https://pan..com/s/1qHFPL1ey8pgVXycUD-LOLQ
--來自網路網盤超級會員v3的分享
如果有另外想要的資源或者鏈接失效,可以使用網路網盤推出的【收集文件】功能進行提問收集資源哦,無需轉存簡單快捷,功能鏈接:https://pan..com/disk/main#/transfer/list微信小程序、網路網盤app、web端都已上線
Ⅳ 用Python如何提取文本文件的特定數據
用csv模塊能解決你的問題,或者嘗試下面的代碼:
withopen(filepath,"r")asfr:
forlineinfr:
line=line.spilt()
printline[3:6]
Ⅵ python處理txt文本,提取其中的DN值、OPTRCL值、DIV值、 LNATT值跟COS值
import re
logfile = open("file.txt","r") # file.log 就是你的日誌名
outfile = open("out.txt","w") # out.txt 就是輸出文件
while True:
out = logfile.readline()
dn = out.find ("DN") #在每行中查找 DN 字元串
op = out.find("OPTRCL") #在每行中查找 OPTRCL 字元串
div = out.find("DIV") #在每行中查找 DIV 字元串
lna = out.find("LNATT") #在每行中查找 LNATT 字元串
cos = out.find("COS") #在每行中查找 COS 字元串
if dn > 0 :
outfile.wirte(out[dn:]+r"\d\d\d\d\d\d\d") #輸出 DN 之後的值
if op > 0 :
outfile.wirte(out[op:]+r"\d\d\d\d\d\d\d") #輸出 OPTRCL 之後的值
if div > 0 :
outfile.wirte(out[div:]+r"\d\d\d\d\d\d\d") #輸出 DIV 之後的值
if ina > 0 :
outfile.wirte(out[ina:]+r"\d\d\d\d\d\d\d") #輸出 LNATT 之後的值
if cos > 0 :
outfile.wirte(out[cos:]+r"\d\d\d\d\d\d\d") #輸出 COS 之後的值
if not out :
break
logfile.close()
outfile.close()
我這個是用我自己之前用過的。只提取一個值的程序擴展出來的,不知道能不能跑通。
如果方便的話,你可以傳一個 TXT 文件給我 做做測試。希望能幫到你!
Ⅶ python用腳本程序處理文本數據,提取。
spe=raw_input("inputyourorder")
n=0
withopen("1.txt",r)asA:
foreachlineinA:
tmp=eachline.rstrip().split(" ")
if(tmp[2]==spe):
n+=1
printn,tmp[5],tmp[1]," "
差不多這意思?就是把第三列為某個分類的物種全輸出來。