简体   繁体   English

在python上读取txt文件的列

[英]Reading columns of a txt file on python

I am working with a .txt file.我正在使用 .txt 文件。 This has 100 rows and 5 columns.这有 100 行和 5 列。 I need to divide it in five vectors of lenght 100, one for each column.我需要将它分成五个长度为 100 的向量,每列一个。 I am trying to follow this: Reading specific columns from a text file in python .我试图遵循这一点: 从 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()

I don't know how this is stored.我不知道这是如何存储的。 If I write print('resulttoken') , nothing appears on my screen.如果我写print('resulttoken') ,我的屏幕上不会出现任何内容。

Can someone please tell me what I am doing wrong?有人可以告诉我我做错了什么吗?

Thanks.谢谢。 part of my text file我的文本文件的一部分

x.split(' ') is not useful, because columns of your text file separated by more than one space. x.split(' ')没有用,因为文本文件的列由多个空格分隔。 Use x.split() to ignore spaces:使用x.split()忽略空格:

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)

Well, the file looks like to be split by table rather than space, so try this:好吧,文件看起来像是按表而不是空间分割的,所以试试这个:

token = open('token_data.txt','r')
linestoken=token.readlines()
tokens_column_number = 1 resulttoken=[] for x in linestoken:
    resulttoken.append(x.split('\t'))
token.close()
print(resulttoken)

You want a list of five distinct lists, and append to each in turn.您需要一个包含五个不同列表的列表,并依次附加到每个列表。

columns = [[]] * 5
with open('token_data.txt','r') as token:
    for line in token:
        for field, value in enumerate(line.split()):
             columns[field].append(value)

Now, you will find the first value from the first line in columns[0][0] , the second value from the first line in columns[1][0] , the first value from the second line in columns[0][1] , etc.现在,您将在columns[0][0]找到第一行中的第一个值、 columns[1][0]中第一行中的第二个值、 columns[0][1]第二行中的第一个值columns[0][1]等。

To print the value of a variable, don't put quotes around it.要打印变量的值,不要在它周围加上引号。 Quotes create a literal string.引号创建一个文字字符串。

print(columns[0][0])

prints the value of columns[0][0] whereas打印columns[0][0]的值,而

print('columns[0][0]')

simply prints the literal text "columns[0][0]".简单地打印文字文本“columns[0][0]”。

You can use data_py package to read column wise data in FORTRAN style.您可以使用 data_py 包以 FORTRAN 样式读取列数据。 Install this package using使用安装这个包

pip install data-py

Usage Example使用示例

from data_py import datafile
NoOfLines=0   
lineNumber=2  # Line number to read (Excluding lines starting with '#')
df1=datafile("C:/Folder/SubFolder/data-file-name.txt")
df1.separator=","  # No need to specify if separator is space(" ") and for 'tab' separated values use '\t'
NoOfLines=df1.lines  # Total number of lines in the data file (Excluding lines starting with '#')
[Col1,Col2,Col3,Col4,Col5]=["","","","",""]  # Initial values
[Col1,Col2,Col3,Col4,Col5]=df1.read([Col1,Col2,Col3,Col4,Col5)],lineNumber)
print(Col1,Col2,Col3,Col4,Col5)  # In str format

For details please follow the link https://www.respt.in/p/python-package-datapy.html详情请点击链接https://www.respt.in/p/python-package-datapy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM