導航:首頁 > 編程語言 > python查找文本內容

python查找文本內容

發布時間:2022-03-05 00:12:17

A. 怎麼用python搜索文本並篩選出來

txtfile=open(r'test.txt',"r")
newtxtfile=open(r'new.txt',"w")
linelist=[]
forlineintxtfile:
linelist.append(line)
iflen(linelist)==4:
ifnotlinelist[1].startswith(r'aaa'):
newtxtfile.writelines(linelist)
linelist=[]
iflen(linelist)>1:
ifnotlinelist[1].startswith(r'aaa'):
newtxtfile.writelines(linelist)
eliflen(linelist)==1:
newtxtfile.writelines(linelist)
txtfile.close()
newtxtfile.close()

讀取文件test.txt,將每四行中第二行以aaa開始的去除,寫入新文件new.txt中

B. python讀取txt文件,查找到指定內容,並做出修改

defmodifyip(tfile,sstr,rstr):
try:
lines=open(tfile,'r').readlines()
flen=len(lines)-1
foriinrange(flen):
ifsstrinlines[i]:
lines[i]=lines[i].replace(sstr,rstr)
open(tfile,'w').writelines(lines)

exceptException,e:
printe

modifyip('a.txt','a','A')

C. python 查找txt文件內指定字元串後空格內的內容,正則表達式

importre
text=open(r"a.txt").read()
findtext=re.findall(r"abcs+(defg)",text)
newtext=open(r"b.txt","w")
newtext.writelines(line+" "forlineinfindtext)

D. python 文本查找

這個很簡單哈,我用java寫過類似的,python下沒寫過,但思路都是一樣的,我說一下思路,供你參考一下:
【笨方法】」字元串截取「
基本字元串1=」abc123「
基本字元串2=」345aaa「
例如:目標字元串為:Today
is
a
good
day
aaa123目標字元串345aaa
那麼:
獲得基本字元串1的長度:len1=len(基本字元串1)
獲得基本字元串2的長度:len2=len(基本字元串2)
---------------------------------------------------------------------
以len1長度開始截取目標字元串,以上面的例子為例,截取出來的應該為:
Today_
oday_i
day_is
ay_is_
y_is_a
等...........................
..............
當然這些都是一個循環就可以搞定,然後在這個循環里,對每次接觸的字元串進行比對,如果找到與目標字元串形同的,則記下」索引「
開始進行下一步處理:截取本句剩下的部分,找到」基本字元串2「,然後記下其開始」索引「,那麼兩個」索引「之間的東東就是你想要的那個」目標字元串「,之後你想用它干什麼都行...........
【超簡單的方法】
會」正則表達式「嗎?會的話,直接用正則吧,幾句就出來了..........

E. python怎麼提取出文件里的指定內容

python讀取文件內容的方法:

一.最方便的方法是一次性讀取文件中的所有內容並放置到一個大字元串中:

all_the_text = open('thefile.txt').read( )
# 文本文件中的所有文本
all_the_data = open('abinfile','rb').read( )
# 二進制文件中的所有數據

為了安全起見,最好還是給打開的文件對象指定一個名字,這樣在完成操作之後可以迅速關閉文件,防止一些無用的文件對象佔用內存。舉個例子,對文本文件讀取:

file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

不一定要在這里用Try/finally語句,但是用了效果更好,因為它可以保證文件對象被關閉,即使在讀取中發生了嚴重錯誤。

二.最簡單、最快,也最具Python風格的方法是逐行讀取文本文件內容,並將讀取的數據放置到一個字元串列表中:list_of_all_the_lines = file_object.readlines( )

這樣讀出的每行文本末尾都帶有" "符號;如果你不想這樣,還有另一個替代的辦法,比如:
list_of_all_the_lines = file_object.read( ).splitlines( )
list_of_all_the_lines = file_object.read( ).split(' ')
list_of_all_the_lines = [L.rstrip(' ') for L in file_object]

F. Python 截取文本內容

你要的內容說得不清楚:

importjsonasjs

file="test.json"#此文件中存放的是下面str_js中一樣的內容

str_js='{"msg":"你電腦打字一分鍾字速多少","type":"text"}'#字元串

mydict=js.loads(str_js)

print(mydict['msg'])#你電腦打字一分鍾字速多少

withopen(file)asf:

mydict=js.load(f)

print(mydict['msg'])

不知道你的原始數據內容來自哪裡,是個字典還是字元串?

G. python查找txt文件中關鍵字

偽代碼:

1、遍歷文件夾下所有txt文件

rootdir='/path/to/xx/dir'#文件夾路徑
forparent,dirnames,filenamesinos.walk(rootdir):
forfilenameinfilenames:

2、讀取txt文件里的內容,通過正則表達式把txt里多篇文章拆分開來。得到一個列表:['{xx1}##NO','{xx2}','{xx3}##NO']

3、把上面得到的list寫到一個新的臨時文件里,比如:xx_tmp.txt,然後:shutil.move('xx_tmp.txt','xx.txt')覆蓋掉原來的文件

H. python 文本文件中查找指定的字元串

def find(lists):
for list0 in lists:
if list0.find('set internet Active')>=0:
if list0.find('#')>=0:
continue
else:
return 0 #有一行不帶#號的set internet Active,那麼返回0
return -1 #若沒有不帶號的set internet Active,那麼返回-1

if __name__=='__main':
lists = ['set internet Active','#set internet Active','# set internet Active']
#lists 是從文件中讀出內容的列表
findout=find(lists) #調用函數
print(findout) #列印結果

I. 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查找文本內容相關的資料

熱點內容
java內存緩存 瀏覽:478
安卓手機如何打開hex文件 瀏覽:39
重啟python命令 瀏覽:604
伺服器地址和埠怎麼登錄 瀏覽:679
遼寧監管app在哪裡下載 瀏覽:735
dos命令剪切文件 瀏覽:939
編譯符號表去掉名字 瀏覽:29
查徵信的app哪個最好 瀏覽:664
java圖片文字水印 瀏覽:3
春運期間機場加密 瀏覽:378
androidopengl紋理貼圖 瀏覽:576
老程序員預編譯 瀏覽:79
定民宿哪個APP好 瀏覽:461
維熱納爾方針加密的密文字元概率 瀏覽:374
程序員腰椎鍛煉 瀏覽:149
c編譯器依賴操作系統嗎 瀏覽:240
九年級數學上冊課本pdf 瀏覽:523
蘋果自帶取圖書app如何使用 瀏覽:968
linuxwindows雙系統時間 瀏覽:988
java切水果 瀏覽:431