Python csv 阅读器表现得很奇怪

发布于 2024-09-02 10:25:03 字数 1843 浏览 5 评论 0原文

那么好吧,如果我运行这个错误的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2024-09-09 10:25:03

输出包含每行的结果,因为您要打印每行的第一列和第九列。

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 in row1 if row1 doesn't have any items to access. Check row1 on each loop to see if it has anything in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文