【发布时间】:2017-03-20 04:06:27
【问题描述】:
这是我文件的开头:
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
这是一个示例,但没有所有循环 http://pastebin.com/7GepdJSu
【问题讨论】:
-
请提供所需输出的示例。
-
所以您基本上希望
products成为dict而不是list? -
您最后的
print行需要缩进。 -
如果你没有使用the
csvmodule,那么你就没有解析 CSV。请使用该模块。至于你的问题:你想使用 CSV 文件中的标题吗?还是直接忽略? -
print('GTIN {} Description {} Price £{}'.format(item[0],item[1],item[2]))?
标签: python