簡體   English   中英

如何從python中的txt逐列讀取數據表

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

所以基本上我需要讀取一個文件,並逐列顯示結果,下面顯示示例輸入和輸出以及我的代碼。

這是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"

這是輸出:

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

下面是我的代碼,我陷入了如何逐列打印表格的麻煩:

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()

下面是我的操作:newlist.sort(),名稱列已排序,但ID列未排序。 所有這些值都從txt文件讀取。 我不明白為什么僅ID列未排序?

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

我嘗試使用“ str()”轉換字符串,但結果是相同的

另一個提示...如果要遍歷列而不是行,請使用zip轉置數據。 我將留給您以正確的格式獲取數據:

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

輸出量

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

以上假設您將print()用作函數來判斷Python 3 ...

您可以使用python內置的csv模塊,為自己節省很多討厭的代碼。

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

這將為您提供一個對象,您可以對其進行迭代以獲得值的決定。

>>> 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'}

你可以得到標題列表

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

好的,這里有一些提示:

>>> 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM