python文件处理
我正在使用Python 3.3 Windows。python文件处理,python,string,file,Python,String,File,我正在使用Python 3.3 Windows。 我想编写一个脚本,在文本文件中查找逗号,并在逗号之间显示单词 像 我想知道如何将逗号之间的值与我给出的值进行匹配。 例:我给了John,程序在文件中找到了它,我想也能找到这个单词,即使它是一个倒转的单词,比如nohj,而不是John使用str.split(,”)用逗号分割字符串。这将产生一个列表。然后您可以处理它: contents = open('textfile','r') [re.search('John', p) for p in con
我想编写一个脚本,在文本文件中查找逗号,并在逗号之间显示单词
像 我想知道如何将逗号之间的值与我给出的值进行匹配。
例:我给了
John
,程序在文件中找到了它,我想也能找到这个单词,即使它是一个倒转的单词,比如nohj
,而不是John使用str.split(,”)
用逗号分割字符串。这将产生一个列表。然后您可以处理它:
contents = open('textfile','r')
[re.search('John', p) for p in contents.read().split(',')]
contents.close()
def process(s):
if s.strip() == "John":
print("Hi John")
# do something interesting
data = open("/path/to/file.txt", "r").read()
map(process, data.split(","))
使用str.split(“,”)
以逗号分隔字符串。这将产生一个列表。然后您可以处理它:
def process(s):
if s.strip() == "John":
print("Hi John")
# do something interesting
data = open("/path/to/file.txt", "r").read()
map(process, data.split(","))
试试这个
def get_chars(string):
l = []
for c in string:
if c not in l:
l.append(c)
return sorted(l)
with open('filename','r') as f:
data = f.read()
data = [i.strip().lower() for i in data.split(',')]
search = input('Word to find: ').strip().lower()
for i in data[:]:
if get_chars(i) == s_chars:
print i
试试这个
def get_chars(string):
l = []
for c in string:
if c not in l:
l.append(c)
return sorted(l)
with open('filename','r') as f:
data = f.read()
data = [i.strip().lower() for i in data.split(',')]
search = input('Word to find: ').strip().lower()
for i in data[:]:
if get_chars(i) == s_chars:
print i
你做了什么?我们不是来做你的工作的,告诉我们你做了什么,我们会帮助你。也请阅读此->我只打开了文件,我试图在文件文本中找到kommas。不完全反转我想知道单词中是否有特定的字母。你做了什么?我们不是来做你的工作的,告诉我们你做了什么,我们会帮助你。也请阅读此->我只打开了文件,我正在尝试在文件文本中查找kommas。不完全相反,我想知道该单词在itThanks中是否有特定的字母,工作起来很有魅力,但如何更改if I==搜索,以便在我给出混合字母时找到该单词。@user2149541更新!!!请看更新后的答案。谢谢,效果很好,但是如果I==搜索在我给出混合字母时找到单词,我该如何更改它。@user2149541 Update!!!看看最新的答案。