线不会分裂成阵列

时间:2015-05-16 00:16:12

标签: 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: 'file' object has no attribute 'split'

我的代码在这里:

if split[0] == "/mount":
            extract(split[1])
            with open("info.m") as f:
                f.readlines()
                print f
                e = f.split('/')
                newlang = Conlang(e[0],e[1],e[2],e[3])
                f.close()
                print "Mounted as %s" % (e[0])

如果您知道为什么它不会分裂,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

也许你的意思是这样的:

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分割为一个行列表,它是一个文件对象(也是一个迭代器)。然后它将迭代该列表并打印每一行(以及更多......)。