Python 线不会分裂成数组
从文件中获取行并拆分时,它告诉我:Python 线不会分裂成数组,python,file,python-2.7,io,Python,File,Python 2.7,Io,从文件中获取行并拆分时,它告诉我: Traceback (most recent call last): File "C:\Users\service user\Desktop\cmd\main.py", line 94, in <module> start() File "C:\Users\service user\Desktop\cmd\main.py", line 66, in start e = f.split('/') AttributeError:
Traceback (most recent call last):
File "C:\Users\service user\Desktop\cmd\main.py", line 94, in <module>
start()
File "C:\Users\service user\Desktop\cmd\main.py", line 66, in start
e = f.split('/')
AttributeError: 'file' object has no attribute 'split'
如果您知道它为什么不会分裂,我们将不胜感激。也许您的意思是:
with open("info.m") as f:
# now we have a file object 'f', we want to iterate it's lines
for line in f.readlines():
print line
e = line.split('/')
# ... rest of code
此代码将
f
,它是一个文件对象(也是一个迭代器)拆分为一个行列表。然后它将迭代该列表并打印每一行(以及更多…。谢谢!!我真的很感激!
with open("info.m") as f:
# now we have a file object 'f', we want to iterate it's lines
for line in f.readlines():
print line
e = line.split('/')
# ... rest of code