Python 列表的标题
以下是我的文件的开头:Python 列表的标题,python,Python,以下是我的文件的开头: print("Welcome to Ebs Corp.") print('Please browse my shopping list\n') with open('./Catalouge2.csv', 'r') as ebsfile: products = [] for line in ebsfile: products.append(line.strip().split(',')) for item in products:
print("Welcome to Ebs Corp.")
print('Please browse my shopping list\n')
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文件是:
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”,然后是“描述”,然后是“价格”,我希望它们是一致的。谢谢
我想让它看起来像
GTIN---------------Description-----------------Price
12345678-----------Blue-Eyes White Dragon------5.60
87654321-----------Dark Magician---------------3.20
下面是一个示例,但如果没有所有循环,则应使用。您需要的格式可以使用内置的。我假设您的csv中有一个标题行,如下所示
GTIN、描述、价格
导入csv
>>>打印格式='|{0:请提供所需输出的示例。因此,您基本上希望
产品
成为一个目录
,而不是列表
?您的最后一行打印
需要缩进。如果您不使用,则您没有解析CSV。请使用该模块。至于您的问题:您想使用CSV文件中的头吗?还是仅仅使用CSV文件忽略它?print('GTIN{}描述{}价格{}。格式(项目[0],项目[1],项目[2]))
?我的csv文件中没有标题行。我想在python中使用它。我也不希望破折号出现在那里,我有点希望它看起来像这样,如果它有用的话。但是没有所有的loops@EbrahimMohammed我已经按照你的pastebin格式更新了答案。请阅读我提到的链接,并根据要求进行更改。谢谢s、 现在明白了。我对这个问题还不熟悉,但现在有没有办法删除整个问题?谢谢你的帮助
>>> 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 |