def regexread():
import re
result = ''
savefileagain = open('sliceeverfile3.txt','w')
#text=open('emeverslicefile4.txt','r')
text='09,11,14,34,44,10,11, 27886637, 0\n561, Tue, 5,Feb,2013, 06,25,31,40,45,06,07, 19070109, 0\n560, Fri, 1,Feb,2013, 05,21,34,37,38,01,06, 13063500, 0\n559, Tue,29,Jan,2013,'
pattern='\d\d,\d\d,\d\d,\d\d,\d\d,\d\d,\d\d'
#with open('emeverslicefile4.txt') as text:
f = re.findall(pattern,text)
for item in f:
print(item)
savefileagain.write(item)
#savefileagain.close()
上面写的函数解析文本并返回七个数字的集合。我有三个问题。
TypeError expected string or buffer
,即使通过阅读一些文章,我也无法解决这个问题。这是我第一次用正则表达式,所以请温柔一点!
你走对了路。。。
您将遍历该文件: How to iterate over the file in python
并将regex应用于每一行。当你意识到你在写“item”的时候,上面的链接应该能回答你所有的3个问题,而这个“item”并不存在于这个循环之外。
这应该是个诀窍,查看注释以了解我在这里做什么的解释=) 祝你好运
相关问题 更多 >
编程相关推荐