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
假设您想要找到
''
符号中包含的所有单词,它们正好是两个字符长:这应该是有效的:
使用
^{pr2}$regex
:相关问题 更多 >
编程相关推荐