【发布时间】:2021-08-07 13:29:00
【问题描述】:
gene=[]
x=1
path = '/content/drive/MyDrive/abc.txt'
file = open(path,'r').readlines()
for i in file[x]:
i.split('\t')[0]
gene.append(i)
x+=1
print(gene)
我的输出是
['A', 'H', 'Y', '3', '9', '2', '7', '8', '\t', '1', '4', '5', '.', '5', '4', '4', '\t', '1', '3', '5', '.', '2', '4', '\t', '5', '6', '.', '5', '1', '3', '8']
但我希望它在 '/t' 处拆分,取列表中的第一个元素 AHY392978 并将其附加到列表基因中
['AHY39278', 'AHY39278']
知道如何将元素连接在一起吗?
【问题讨论】:
-
i.split('\t')[0]什么都不做…… -
你只是用
file[x]逐个字符地循环文件的第二行。x+=1什么都不做,因为之后再也没有使用过。 -
在我看来你可以跳过整个循环,直接使用
file。这已经是文件中每一行的列表。 -
@Andreas 我已经编辑了我的问题,希望你可以重新打开它
标签: python string list split append