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搜标题即可找到帖子。