简体   繁体   English

如何从python中的txt逐列读取数据表

[英]How to read data table column by column from a txt in python

so basically I need to read a file in, and display the result column by column, and example input and output is shown below, along with the my code. 所以基本上我需要读取一个文件,并逐列显示结果,下面显示示例输入和输出以及我的代码。

this is the txt file: 这是txt文件:

  Name  ID  City    Favorite Fruit
Benjamin    5   Copenhagen  kiwi
Tom 100 Kingston    "watermelon, apple"
Rosemary    20  Philadelphia    "pineapple, mango"
Annie   95  East Setauket   "blueberry, hawthorn"
Jonathan    75  Ithaca  cherry
Kathryn 40  San Francisco   "banana, strawberry"

and this is the output: 这是输出:

Number of rows: 7
Number of columns: 4
Column 0: Name
 1 Annie
 1 Benjamin
 1 Jonathan
 1 Kathryn
 1 Rosemary
 1 Tom
Column 1: ID
 1 5
 1 20
 1 40
 1 75
 1 95
 1 100
Column 2: City
1 Copenhagen
1 East Setauket
1 Ithaca
1 Kingston
1 Philadelphia
1 San Francisco
Column 3: Favorite Fruit
 1 "banana, strawberry"
1 "blueberry, hawthorn"
 1 "pineapple, mango"
 1 "watermelon, apple"
 1 cherry
 1 kiwi

and the below is my code, i got stuck at how to print the table out column by column: 下面是我的代码,我陷入了如何逐列打印表格的麻烦:

import sys
def main():
    alist =[]
    data = open("a1input1.txt").read()
    lines = data.split('\n')
    totalline =len(lines)
    print ("Number of low is: " + str(totalline))
    column = lines[0].split('\t')
    totalcolumn = len(column)
    print ("Number of column is: " + str(totalcolumn))
    for index in range(totalline):
        column = lines[index].split('\t')
        print (column)
 main()

below is what I got doing: newlist.sort(), the name column is sorted, but the ID column is not. 下面是我的操作:newlist.sort(),名称列已排序,但ID列未排序。 all these vales are reading from a txt file. 所有这些值都从txt文件读取。 I don't get why only the ID column is not sorted? 我不明白为什么仅ID列未排序?

Column 0: Name
Annie
Benjamin
Jonathan
Kathryn
Rosemary
Tom
Column 1: ID
100
20
40
5
75
95

I have tried to convert the string using the "str()", but the result is the same 我尝试使用“ str()”转换字符串,但结果是相同的

Another hint... If you want to iterate over columns instead of rows, transpose the data using zip . 另一个提示...如果要遍历列而不是行,请使用zip转置数据。 I'll leave it up to you to get the data in the right format: 我将留给您以正确的格式获取数据:

data = [['a','b','c'],[1,2,3],[4,5,6],[7,8,9]]
print(data)
data = list(zip(*data))
print(data)

Output 输出量

[['a', 'b', 'c'], [1, 2, 3], [4, 5, 6], [7, 8, 9]]
[('a', 1, 4, 7), ('b', 2, 5, 8), ('c', 3, 6, 9)]

The above assumes Python 3 judging by your use of print() as a function... 以上假设您将print()用作函数来判断Python 3 ...

You can use python in-built csv module and save yourself a lot of nasty looking code. 您可以使用python内置的csv模块,为自己节省很多讨厌的代码。

import csv
data = open("data", "rb")
csv_dict = csv.DictReader(data, delimiter="\t", quotechar="\"")

This will give you an object that you can iterate over to get a dict of the values. 这将为您提供一个对象,您可以对其进行迭代以获得值的决定。

>>> for item in csv_dict:
...     print item
... 
{'City': 'Copenhagen', 'Favorite Fruit': 'kiwi', 'Name': 'Benjamin', 'ID': '5'}
{'City': 'Kingston', 'Favorite Fruit': 'watermelon, apple', 'Name': 'Tom', 'ID': '100'}
{'City': 'Philadelphia', 'Favorite Fruit': 'pineapple, mango', 'Name': 'Rosemary', 'ID': '20'}
{'City': 'East Setauket', 'Favorite Fruit': 'blueberry, hawthorn', 'Name': 'Annie', 'ID': ' 95'}
{'City': 'Ithaca', 'Favorite Fruit': 'cherry', 'Name': 'Jonathan', 'ID': '75'}
{'City': 'San Francisco', 'Favorite Fruit': 'banana, strawberry', 'Name': 'Kathryn', 'ID': '40'}

and you can get a list of the headers 你可以得到标题列表

>>> csv_dict.fieldnames
['Name', 'ID', 'City', 'Favorite Fruit']

Ok, here are some hints: 好的,这里有一些提示:

>>> s = 'a \tb \tc \td\ne \tf \tg \th'
>>> s.split()
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> s.split('\n')
['a \tb \tc \td', 'e \tf \tg \th']
>>> rows = [x.split() for x in s.split('\n')]
>>> rows
[['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']]
>>> [row[0] for row in rows]
['a', 'e']
>>> [row[1] for row in rows]
['b', 'f']

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

相关问题 如何将url中的表读取为DataFrame并修改Python中某一列数据的格式 Pandas? - How to read table from url as DataFrame and modify format of data in one column in Python Pandas? 如何从python中的特定列读取Excel数据 - How to read data from excel from a particular column in python 如何从csv读取没有标题的列并使用Python将输出保存在txt文件中? - How to read a column without header from csv and save the output in a txt file using Python? 当列名与 Pandas 的数据内联时,如何读取 .txt 文件? - How to read in a .txt file when the column names are inline with the data with Pandas? 如何使用Python中的行和列数据从文本文件读取值 - How to read values from text file, with row and column data in Python 如何使用 python 保存 txt 文件并制作两列表? - How to save txt file and make two column table using python? 只读txt文件中的第一列 - Read only first column from txt file 如何通过python(来自txt文件)将很长的行数据分成两列 - how to split a very long row data into two column by python (from txt file) 如何使用python pandas从未命名列excel中过滤包含关键字的文本数据并打印到txt文件 - How to filter text data containing key words from an unnamed column excel with python pandas and print to txt file 如何使用 python 读取 excel 列数据并打印列重复 - How to use python to read excel column data and print column duplicates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM