在python上读取txt文件的列 [英] Reading columns of a txt file on python
问题描述
我正在处理一个 .txt 文件.这有 100 行和 5 列.我需要将它分成五个长度为 100 的向量,每列一个.我正在尝试遵循:从文本文件中读取特定列在python中.
I am working with a .txt file. This has 100 rows and 5 columns. I need to divide it in five vectors of lenght 100, one for each column. I am trying to follow this: Reading specific columns from a text file in python.
但是,当我将其实现为:
However, when I implement it as:
token = open('token_data.txt','r')
linestoken=token.readlines()
resulttoken=[]
for x in linestoken:
resulttoken.append(x.split(' ')[1])
token.close()
我不知道这是如何存储的.如果我写 print('resulttoken')
,屏幕上不会出现任何内容.
I don't know how this is stored. If I write print('resulttoken')
, nothing appears on my screen.
谁能告诉我我做错了什么?
Can someone please tell me what I am doing wrong?
谢谢.我的文本文件的一部分
推荐答案
x.split(' ')
没有用,因为文本文件的列被多个空格分隔.使用 x.split()
忽略空格:
x.split(' ')
is not useful, because columns of your text file separated by more than one space. Use x.split()
to ignore spaces:
token = open('token_data.txt','r')
linestoken=token.readlines()
tokens_column_number = 1
resulttoken=[]
for x in linestoken:
resulttoken.append(x.split()[tokens_column_number])
token.close()
print(resulttoken)
这篇关于在python上读取txt文件的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!