所以我试图通过将文本文件转换为列表并在空间处拆分列表中的每个项目来解析文本文件。
我创建了一个测试变量来自行运行这部分代码。我在 spyder 编辑器中的代码:
test = ['NC_009142.1_03_012_002_001 560', 'NC_017586.1_13_009_003_001 555', 'NC_016111.1_13_010_003_001 555']
ListOfLinesParsed = test
PN_List = []
counter_iterative = 0
while counter_iterative < len(ListOfLinesParsed):
PN_List = PN_List.append(ListOfLinesParsed[counter_iterative].split()[0])
counter_iterative += 1
print PN_List
返回错误:
运行文件(r'/home/jake/.spyder2/.temp.py',wdir=r'/home/jake/.spyder2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-
packages/spyderlib/widgets/externalshell/sitecustomize.py", line 493, in runfile
execfile(filename, namespace)
File "/home/jake/.spyder2/.temp.py", line 7, in <module>
PN_List = PN_List.append(ListOfLinesParsed[counter_iterative].split()[0])
AttributeError: 'NoneType' object has no attribute 'append'
但是,如果我将命令直接输入到终端中,则不会出现错误:
测试L = []
testL.append(test[0].split()[0])
测试L
['NC_009142.1_03_012_002_001']
testL.append(test[1].split()[0])
测试L
['NC_009142.1_03_012_002_001', 'NC_017586.1_13_009_003_001']
testL.append(test[2].split()[0])
测试L
['NC_009142.1_03_012_002_001', 'NC_017586.1_13_009_003_001', 'NC_016111.1_13_010_003_001']
这两件事不应该完全相同吗?我不明白为什么我的脚本中的行为与终端命令有任何不同。