我有这段代码:
import re
with open("text2.txt", "r") as f:
content = f.readlines()
numbers = re.findall(r'\b\d{3}\b', content)
with open("text3.txt", "w") as f:
f.write(str(numbers))
运行时,它应该找到所有三位数字,然后将它们打印到新的文本文件中。
当我运行它时,我收到此错误:
Traceback (most recent call last):
File "C:\Users\Zach\Desktop\test3.py", line 4, in <module>
numbers = re.findall(r'\b\d{3}\b', content)
File "C:\Panda3D-1.7.2\python\lib\re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
我做错了什么?