【发布时间】:2017-01-27 08:55:58
【问题描述】:
我有一个包含数据的文件:
ABC acd IGK EFG
GHQ ghq acb efg
IJK ijk gtt ttg
我想拆分它的行并从每行中获取一些数据并将它们加入一个列表。像这样:
a = ['acd', 'ghq', 'ijk']
到目前为止,我已经完成了以下操作。
li = []
with open('file.txt') as fl:
for f in fl:
f = f.split()
li = li.append(f[2])
但我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AttributeError: 'NoneType' object has no attribute 'append'
有人可以帮我完成代码吗?
【问题讨论】:
-
诸如
lists 和dicts 等可变对象的方法在原地更改它,例如list.append()和dict.clear(),不返回被更改的对象。他们有效地返回None。
标签: python list file split append