當前位置:首頁 » 編程語言 » python文件包含字元串

python文件包含字元串

發布時間: 2022-03-09 12:45:14

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

  1. 我都沒用過listdit。

  2. 但是,去找了下其使用說明:

  3. os.listdir(path)
    .Thelistisinarbitraryorder.Itdoesnotincludethespecial
    entries'.'and'..'eveniftheyarepresentinthe
    directory.
    Availability:Unix,Windows.
    Changedinversion2.3:OnWindowsNT/2k/XPandUnix,ifpathisaUnicodeobject,theresultwillbe
    alistofUnicodeobjects.
    stringobjects.

    所以:

  4. 你可以試試,傳入路徑是unicode,比如:

  5. foundDirList=os.listdir(u"在這里輸入你的")
    然後,輸出的list中的文件名列表,就都是unicode了,就可以正常顯示出你要的,包括特殊字元的文件名了。
  6. 然後你就可以正常的打開了。

  7. 當然,後續處理文件時,如果是中文等非ASCII的話,也是要了解涉及到字元編碼的。這時候,最好用codecs模塊。如何使用,參見:

  8. 【教程】用Python的codecs處理各種字元編碼的字元串和文件
    這里不能貼地址,google搜標題即可找到帖子。
熱點內容
密碼忘記了怎麼找回 發布:2025-07-20 12:46:18 瀏覽:531
華為的密碼鎖為什麼開不了 發布:2025-07-20 12:45:35 瀏覽:132
app登錄密碼在哪裡 發布:2025-07-20 12:22:31 瀏覽:692
python私有成員變數 發布:2025-07-20 12:03:05 瀏覽:412
創建表的存儲過程 發布:2025-07-20 12:01:27 瀏覽:856
安卓怎麼給girl999投票 發布:2025-07-20 11:53:58 瀏覽:568
linux卸載openoffice 發布:2025-07-20 11:48:42 瀏覽:393
安卓藍牙傳圖片到iphone怎麼失敗 發布:2025-07-20 11:48:41 瀏覽:421
手機低配置怎麼提高配置 發布:2025-07-20 11:41:34 瀏覽:521
小猴編程登錄 發布:2025-07-20 11:40:38 瀏覽:364