【问题标题】:Line wont split into array行不会拆分为数组
【发布时间】:2015-05-16 00:16:11
【问题描述】:

从文件中获取一行并将其拆分时告诉我:

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])

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

【问题讨论】:

    标签: python file python-2.7 io


    【解决方案1】:

    也许你的意思是这样的:

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

    【讨论】:

    • 谢谢!!我真的很感激!
    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多