当前位置:首页 » 编程语言 » python搜索字符串

python搜索字符串

发布时间: 2023-06-09 01:05:07

python字典,如何查找值中包含指定字符串的键

1、说明python中检测字典的键中是否含有某串字符,便利字典键值,再判断字符串是否在键值中即可。2、示例代码:# 定义一个字典dic = {'1984/1/2': 123, '1984/1/3': 0, '1985/1/1': 156}# 遍历字典键中是否包含1984for key in dic: if '1984' in key: print('键值中包含字符串"1984"') # 或者需要的其它操作 else: print('键值中不包含字符串"1984"')3、执行结果:键值中包含字符串"1984"键值中不包含字符串"1984"键值中包含字符串"1984"

4、其它说明:python使用for in直接操作字典就是遍历字典的键值,python使用in操作来判断字符串中是否包含子串最方便,要优于使用字符串的函数index或者find。

index函数在找不到子串时会报错,find函数会返回-1。

❷ 如何用Python语言实现在一个文件中查找特定的字符串

用正则表达式

>>>s='helloworld'
>>>importre
>>>re.search('wor',s)
<_sre.SRE_Matchobject;span=(6,9),match='wor'>

❸ 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()

❹ 如何用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的所有出现的子串的副本旧换新。 如果可选参数计数为给定,只有第一个计数出现被替换。

❺ python 如何连续查找字符串

python的字符串可以看做是数组的

所以比如mystr = "what is your name"

newstr = mystr

if newstr.find("a") >= 0:

newstr = newstr[newstr.find("a"):] #这是后newstr就是第一个a开始之后的字符串

如果不需要包含第一个找到的a,那么可以这样:
while newstr.find("a") >= 0:

newstr = newstr[newstr.find("a")+1:] #这样一致到newstr里面不包含a为止

❻ Python API快餐教程(1) - 字符串查找API

字符串是7种序列类型中的一种。
除了序列的操作函数,比如len()来求字符串长度之外,Python还为字符串提供丰富到可以写个编辑器的API.

首先,下面的查找API都是为了查找位置,或者查一共有几次这样的操作。
如果只是想判断一个字符串是不是另一个字符串的子串的话,使用序列的in运算符就可以了。
例:

原型:str.count(sub[, start[, end]])

字符串的count函数可以数出来有多少次匹配,我们看个例子,有5个ha和3个hei

输出为5和2.
haha只能匹配两次。

再加上扩展参数:

find函数的行为是,如果能找到,则返回在序列中的坐标,如果找不到,则返回-1. rfind是从右向左查找。我们来看例子:

输出值为0和6.

找不到的例子:

输出值都是-1.

完整形式:

index和rindex的功能与find和rfind基本上一致,除了在找不到时会抛出ValueError异常而不是返回-1.

例:

所以我们需要加try...except语句来处理之:

有时候,我们希望做从头匹配或者匹配尾部。这时候就要用到startswith函数和endswith函数。例:

这两个返回值均为True.

如果需要更复杂的匹配,还是需要正则表达式。与Java等语言不同,Python中的正则表达式有专门的模块,字符串的API不负责这个事情。

❼ python 查找字符串并将其替换

f1=open('J:/wenjian/1/1.txt','r')
for line in f1
你这里是不是少了点什么,f1只是文件句柄,需要执行读操作才能遍历,
调用readlines()

确实有更好的代码,那就是使用re.sub,它同时包含了查找和替换两步的操作,
而不是像你写的那样的字符串比较性能那么低

❽ 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) #打印结果

❾ Python中检索字符串的方法有哪些呢

你还可以用更灵活的 regular 正则式
search()和match(),用起来更灵活
import re
str = "Welcome to my world. I have 12 apples."
if re.search(r"world", str).group() != "" :
print("match! ")

str = "abcABC"
if re.match(r"[a-zA-Z]+", str):
print("match! ", re.search(r"[A-Z]+", str).group())
else:
print("ummatch! ")

热点内容
查看linux的shell 发布:2025-02-14 01:38:42 浏览:988
用于打开ftp连接的应用程序 发布:2025-02-14 01:23:39 浏览:706
网站会员注册源码 发布:2025-02-14 01:09:45 浏览:657
小火山视频密码是什么 发布:2025-02-14 01:09:40 浏览:505
我的世界手机创的服务器电脑能进吗 发布:2025-02-14 01:08:16 浏览:163
eclipseandroid运行 发布:2025-02-14 00:54:57 浏览:897
云服务器安全策略 发布:2025-02-14 00:54:07 浏览:289
小米手机如何更改账号密码 发布:2025-02-14 00:48:48 浏览:572
我的世界如何导出服务器 发布:2025-02-14 00:48:39 浏览:722
工业服务器机箱怎么样 发布:2025-02-14 00:29:15 浏览:86