Header for list
本问题已经有最佳答案,请猛点这里访问。
这是我文件的开头:
1 2 3 4 5 6 7 8 9 | print("Welcome to Ebs Corp.") print('Please browse my shopping list ') with open('./Catalouge2.csv', 'r') as ebsfile: products = [] for line in ebsfile: products.append(line.strip().split(',')) for item in products: print('{} {} £{}'.format(item[0],item[1],item[2])) |
我的CSV文件是:
1 2 3 4 5 6 7 | 12345678,Blue-Eyes White Dragon,5.60 87654321,Dark Magician,3.20 24681012,Toon Blue-Eyes White Dragon,2.00 10357911,Toon Dark Magician,3.00 54626786,Duel Mat,4.30 85395634,Deck Box,2.50 78563412,Pot of Greed,10.50 |
我希望它能够为每个项目添加一个标题。对于一开始的数字,我希望它有"gtin",然后是"description",然后是"price",我希望它们是一致的。谢谢
我想让它看起来像
1 2 3 | GTIN---------------Description-----------------Price 12345678-----------Blue-Eyes White Dragon------5.60 87654321-----------Dark Magician---------------3.20 |
下面是一个例子,但是没有所有的循环http://pastebin.com/7gepdjsu
您应该使用
GTIN,description,price
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> import csv >>> print_format = '| {0: <10} | {1: <30} | {2: >5} |' >>> with open('/home/ashish/Desktop/sample.csv') as csvfile: ... reader = csv.reader(csvfile) ... print(print_format.format('GTIN', 'Description', 'Price')) ... for row in reader: ... print(print_format.format(row[0], row[1], row[2])) ... | GTIN | Description | Price | | GTIN | description | price | | 12345678 | Blue-Eyes White Dragon | 5.60 | | 87654321 | Dark Magician | 3.20 | | 24681012 | Toon Blue-Eyes White Dragon | 2.00 | | 10357911 | Toon Dark Magician | 3.00 | | 54626786 | Duel Mat | 4.30 | | 85395634 | Deck Box | 2.50 | | 78563412 | Pot of Greed | 10.50 | |