python查找指定字符串(python 怎么在列表中查找含有某个字符)

本文目录
- python 怎么在列表中查找含有某个字符
- 如何用Python语言实现在一个文件中查找特定的字符串
- Python新手求助,如何查找一个包含指定字符的字符串
- python 文本文件中查找指定的字符串
- 如何用Python来进行查询和替换一个文本字符串
python 怎么在列表中查找含有某个字符
In : data=[’qwerfyy’,’dsfha’,’fdyythg’,’efggg’]
In : [i for i in data if ’yy’ in i]
Out: [’qwerfyy’, ’fdyythg’]
In : import re
In : [i for i in data if re.search(’yy’,i)]
Out: [’qwerfyy’, ’fdyythg’]
如何用Python语言实现在一个文件中查找特定的字符串
targetstr 为特定字符串
filename为文件名
with open(filename,’r’)as fp:
for line in fp:
if targetstr in line:
print line
这样就找到特定字符串所在的行内容了
Python新手求助,如何查找一个包含指定字符的字符串
def findstr(rlist, onestr):
found =
for element in rlist:
if onestr in element:
found.append(element)
return found
参数rlist就是你的[132,135,xxx], onestr就是13.
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):
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()
如何用Python来进行查询和替换一个文本字符串
1、说明
可以使用find或者index来查询字符串,可以使用replace函数来替换字符串。
2、示例
1)查询
》》》 ’abcdefg’.find(’cde’)
结果为2
’abcdefg’.find(’acde’)
结果为-1
’abcdefg’.index(’cde’)
结果为2
2)替换
’abcdefg’.replace(’abc’,’cde’)
结果为’cdedefg’
3、函数说明
1)find(...)
S.find(sub[, start[, end]]) -》 int
返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中。 可选的 参数start和end解释为切片表示法。
失败时返回-1。
2)index(...)
S.index(sub[, start[, end]]) -》 int
与find函数类似,但是当未找到子字符串时引发ValueError。
3)replace(...)
S.replace(old, new[, count]) -》 str
返回S的所有出现的子串的副本旧换新。 如果可选参数计数为给定,只有第一个计数出现被替换。

更多文章:
turtles歌曲(哪位大神有turtles(乌龟组合)的<谢谢>的歌词中文翻译 感激不尽)
2026年10月11日 10:00
金山铁路22号线(请问现在轨道交通22号线金山铁路是个什么情况据说9月28日就开通了啊~~~)
2026年10月11日 09:10
repercussions(都是余波,repercussions和aftermath有什么区别啊)
2026年10月11日 09:00
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40



