Python csv 阅读器表现得很奇怪
那么好吧,如果我运行这个错误的代码:
csvReader1 = csv.reader(file('new_categories.csv', "rU"), delimiter=',')
for row1 in csvReader1:
print row1[0]
print row1[8]
category_sku = str(row[8])
if category_sku == sku:
classifications["Craft"] = row[0]
classifications["Theme"] = row[1]
我得到:
Knitting
391
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 403, in <module>
inventory_item_list = get_item_list(product)
File "upload_all_inventory_ebay.py", line 294, in get_item_list
category_sku = str(row[8])
NameError: global name 'row' is not defined
Where Knitting 和 391 完全正确,当然我需要将 row[8] 引用为 row1[8]...k 所以我这样做:
csvReader1 = csv.reader(file('new_categories.csv', "rU"), delimiter=',')
for row1 in csvReader1:
print row1[0]
print row1[8]
category_sku = str(row1[8])
if category_sku == sku:
classifications["Craft"] = row1[0]
classifications["Theme"] = row1[1]
我得到这个:
...........
Crochet
107452
Knitting
107454
Knitting
107455
Knitting
107456
Knitting
107457
Crochet
108200
Crochet
108201
Crochet
108205
Crochet
108213
Crochet
108214
Crochet
108217
108432
Quilt
108451
108482
108488
Scrapbooking
108711
Knitting
122363
Needlework
Beading
Crafts & Decorating
Crochet
Crochet
Crochet
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 403, in <module>
inventory_item_list = get_item_list(product)
File "upload_all_inventory_ebay.py", line 292, in get_item_list
print row1[0]
IndexError: list index out of range
在您看到的输出中,第 0 列和第 1 列中包含所有令人讨厌的东西!!!!!!!!!!!!为什么?如果 row1[0] 之前不在范围内,为什么它会超出范围。耶星期五!
So OK if I run this wrong code:
csvReader1 = csv.reader(file('new_categories.csv', "rU"), delimiter=',')
for row1 in csvReader1:
print row1[0]
print row1[8]
category_sku = str(row[8])
if category_sku == sku:
classifications["Craft"] = row[0]
classifications["Theme"] = row[1]
I get:
Knitting
391
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 403, in <module>
inventory_item_list = get_item_list(product)
File "upload_all_inventory_ebay.py", line 294, in get_item_list
category_sku = str(row[8])
NameError: global name 'row' is not defined
Where Knitting and 391 are exactly right, of course I need to refer to row[8] as row1[8]...k so I do this:
csvReader1 = csv.reader(file('new_categories.csv', "rU"), delimiter=',')
for row1 in csvReader1:
print row1[0]
print row1[8]
category_sku = str(row1[8])
if category_sku == sku:
classifications["Craft"] = row1[0]
classifications["Theme"] = row1[1]
And I get this:
...........
Crochet
107452
Knitting
107454
Knitting
107455
Knitting
107456
Knitting
107457
Crochet
108200
Crochet
108201
Crochet
108205
Crochet
108213
Crochet
108214
Crochet
108217
108432
Quilt
108451
108482
108488
Scrapbooking
108711
Knitting
122363
Needlework
Beading
Crafts & Decorating
Crochet
Crochet
Crochet
Traceback (most recent call last):
File "upload_all_inventory_ebay.py", line 403, in <module>
inventory_item_list = get_item_list(product)
File "upload_all_inventory_ebay.py", line 292, in get_item_list
print row1[0]
IndexError: list index out of range
Where the output you see there is every effing thing in column 0 and column 1 !!!!!!!!!! Why? And WHY is row1[0] out of range if it wasn't before. YAY Fridays!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出包含每行的结果,因为您要打印每行的第一列和第九列。
row1[0]
对于任何行都超出范围,因为文件的该特定行上没有任何项目。如果row1
没有任何可访问的项目,则您无法访问row1
中的第一个项目。检查每个循环上的row1
以查看其中是否有任何内容。The output contains results from every row because you are printing the 1st and 9th columns for every row.
row1[0]
is out of range for whatever row that is because there aren't any items on that particular line of the file. You can't access the first item inrow1
ifrow1
doesn't have any items to access. Checkrow1
on each loop to see if it has anything in it.