Ⅰ 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]," "
差不多这意思?就是把第三列为某个分类的物种全输出来。