0
我有一个文件包含线,如:如何使用python提取一行中的特定数据?
754.82915: MODE1(0, 0x001900, 0x00090)
754.82916: MODE2(0, 0x001900, 0x00090)
如何从取数据“(”到“)”在python ??。
我试过代码:
fo=open("file1.txt","r")
fin=open("file2.txt","w")
lines=fo.readlines()
for line in lines:
result=re.search(r'\(.*\)', line)
res="\n"+result.group()
fin.write(res)
fo.close()
它显示以下错误:
AttributeError: 'NoneType' object has no attribute 'group'
这就是你的文本文件的确切内容? (可能你也有一些空行) –
['re.search(pattern,string,flags = 0)'](http://docs.python.org/2/library/re.html#re。搜索):如果字符串中没有位置与模式匹配,则返回无;请注意,这与在字符串中的某处找到零长度匹配不同。 –