从fi过滤特定长度的字符串

2024-10-06 07:43:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个食品.txt包含内容的文件

'w3ll' 'i' '4m' 'n0t' '4sed' 't0' 

'it'

我试着提取所有有两个字符的单词。我的意思是,输出文件应该只有

^{pr2}$

我试过的是

with open("foo.txt" , 'r') as foo:
    listme = foo.read()

string =  listme.strip().split("'")

我想这会用“符号”来分割字符串。 我怎样才能只选择那些撇号中字符数等于2的字符串?在


Tags: 文件字符串txt食品内容foowithit
3条回答

假设您想要找到''符号中包含的所有单词,它们正好是两个字符长:

import re
split = re.compile(r"'\w{2}'")

with open("file2","w") as fw:
    for word in split.findall(open("file","r").read()):
            fw.write(word.strip("'")+"\n")
with open("foo.txt" , 'r') as file:
  words = [word.strip("'") for line in file for word in line.split() if len(word) == 4]

with open("out", "w") as out:
  out.write('\n'.join(words) + '\n')

这应该是有效的:

>>> with open('abc') as f, open('output.txt', 'w') as f2:
...     for line in f:
...         for word in line.split():    #split the line at whitespaces
...             word = word.strip("'")   # strip out `'` from each word
...             if len(word) == 2:       #if len(word) is 2 then write it to file
...                 f2.write(word + '\n')

print open('output.txt').read()
4m
t0
it

使用regex

^{pr2}$

相关问题 更多 >