python文件包含字元串
1. python re模塊如何判斷字元串中包含某些特定字元如文件名中不能包含'','/'等字元,如何檢查
方法有很多,例如使用首尾位置標記^$+非法字元集[^]實現:
regex=r'^[^\/:*?"<>|]+$'#不能為空,不能含有/:*?"<>|等字元
tests=['abc_def',
'abc.def',
'abc/def',
'?"',
'']
matches=[iforiintestsifre.match(regex,i)]
print(matches)
還可以通過負向零寬斷言(?!)等方式實現。
2. python 怎樣在文件中查找指定的字元串
第一種情況:在python編輯器中找一個字元串string
ctrl+f
第二種情況:判斷元組或列表內是否包含字元串:string in list
3. 怎麼樣使用python3查找文件中是否包含某個字元串,沒有則在文件末尾追加進去
a = 'RQUOTAD_PORT=30001'
with open('nfs','r') as f:
if a not in f.read():
print(a)
f=open('/nfs','a')
f.write("RQUOTAD_PORT=30001
")
f.close()
4. python中判斷在文件中是否存在某字元串
a = 'abc' #--------------------要查詢的字元串 with open('1.txt','r') as foo: for line in foo.readlines(): if a in line: print line
5. python判斷文件內是否存在某字元串
方法:使用 in 方法實現contains的功能:
1 site = 'http://www.jb51.net/'
2 if "jb51" in site:
3 print('site contains jb51')
輸出結果:site contains jb51
6. python中如何判斷指定字元串是否在文件中
#-*-coding:utf-8-*-
__author__='niubiqigai'
executeRecord="niu"
rec=open('py.txt','r+')
lineInfos=rec.readlines()
recordFlag=True
forrowinlineInfos:
print(row.strip().find(executeRecord))
#find函數-1表示找不到匹配內容,其他輸出結果為找到的索引值
ifrow.strip().find(executeRecord)!=-1:
print("%s已經存在!"%(executeRecord))
#記錄過即不再記錄
recordFlag=False
break
ifrecordFlag:
executeRecord='%s '%executeRecord
rec.write(executeRecord)
rec.close()
7. python 文本文件中查找指定的字元串
編寫一個程序,能在當前目錄以及當前目錄的所有子目錄下查找文件名包含指定字元串的文件,並列印出絕對路徑。
import os
class SearchFile(object):
def __init__(self,path='.'):
self._path=path
self.abspath=os.path.abspath(self._path) # 默認當前目錄
def findfile(self,keyword,root):
filelist=[]
for root,dirs,files in os.walk(root):
for name in files:
fitfile=filelist.append(os.path.join(root, name))
#print(fitfile)
print(os.path.join(root, name))
#print(filelist)
print('...........................................')
for i in filelist:
if os.path.isfile(i):
#print(i)
if keyword in os.path.split(i)[1]:
print('yes!',i) # 絕對路徑
#else:
#print('......no keyword!')
def __call__(self):
while True:
workpath=input('Do you want to work under the current folder? Y/N:')
if(workpath == ''):
break
if workpath=='y' or workpath=='Y':
root=self.abspath # 把當前工作目錄作為工作目錄
print('當前工作目錄:',root)
dirlist=os.listdir() # 列出工作目錄下的文件和目錄
print(dirlist)
else:
root=input('please enter the working directory:')
print('當前工作目錄:',root)
keyword=input('the keyword you want to find:')
if(keyword==''):
break
self.findfile(keyword,root) # 查找帶指定字元的文件
if __name__ == '__main__':
search = SearchFile()
search()
8. 如何用python提取文本包含特定字元串的整行並生成到一個新的文本文件里
b=[xforxinopen('a.txt').readlines()ifx.find('<XYZ>')>-1]
withopen('b.txt','w')asf:
f.writelines(b)
9. python 在列表中查找包含所以某個字元串的項,並保存到一個新的列表
# 文件不很大的話:
def findstrinfile(filename, lookup):
return lookup in open(filename,'rt').read()
# 對付大文件:
def findstrinlargefile(filename, lookup):
with open(filename, 'rt') as handle:
for ln in handle:
if lookup in ln:
return True
else:
return False
10. python怎麼讀取文件名中包含特殊字元的文件 比如xiân.txt
我都沒用過listdit。
但是,去找了下其使用說明:
os.listdir(path)
.Thelistisinarbitraryorder.Itdoesnotincludethespecial
entries'.'and'..'eveniftheyarepresentinthe
directory.
Availability:Unix,Windows.
Changedinversion2.3:OnWindowsNT/2k/XPandUnix,ifpathisaUnicodeobject,theresultwillbe
alistofUnicodeobjects.
stringobjects.所以:
你可以試試,傳入路徑是unicode,比如:
foundDirList=os.listdir(u"在這里輸入你的")
然後,輸出的list中的文件名列表,就都是unicode了,就可以正常顯示出你要的,包括特殊字元的文件名了。然後你就可以正常的打開了。
當然,後續處理文件時,如果是中文等非ASCII的話,也是要了解涉及到字元編碼的。這時候,最好用codecs模塊。如何使用,參見:
【教程】用Python的codecs處理各種字元編碼的字元串和文件
這里不能貼地址,google搜標題即可找到帖子。