[英]Error when reading columns in txt file with python
I have a txt file with 1150 lines and 5 columns.我有一个 1150 行 5 列的 txt 文件。 The columns are separated with a tab.
列用制表符分隔。 I want to get arrays containing a certain column of the txt file.
我想获得包含 txt 文件的某一列的 arrays 。 I use the following code to get the columns:
我使用以下代码来获取列:
f = open(file, "r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split('\t')[0])
f.close()
With this I get all the values of my first column, but when I want to get the second column with有了这个,我得到了第一列的所有值,但是当我想得到第二列时
result.append(x.split('\t')[1])
I get an IndexError: list index out of range我得到一个 IndexError: list index out of range
How can I solve this problem?我怎么解决这个问题?
Try printing the lenght of each x within the loop:尝试在循环中打印每个x的长度:
print(len(line.split('\t')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.