简体   繁体   English

字符串索引必须是整数,而不是str-Python脚本

[英]string indices must be integers, not str - Python script

Trying to write a small script to make my life easier as I interface with the warehouse. 尝试编写一个小的脚本,以便在与仓库交互时使我的生活更轻松。 Wanting to make something that will let me export a simple csv file with all the different styles, sizes, colors, and UPC codes of the products I need to fulfill. 想要做一些能让我导出一个简单的csv文件的文件,其中包含我需要满足的产品的所有不同样式,大小,颜色和UPC代码。 Here is part of the code that deals with getting the UPC codes from the dictionary I created: 这是部分代码,用于从我创建的字典中获取UPC代码:

UPC = {'True Premium Flat': {'White': {'6':'994000000446','7':'994000000453','8':'994000000460','9':'994000000477','10':'994000000484','11':'994000000491','12':'994000000507'},
                             'Silver': {'6':'994000000514','7':'994000000521','8':'994000000538','9':'994000000545','10':'994000000552','11':'994000000569','12':'994000000576'},
                             'Champagne': {'6':'994000000309','7':'994000000316','8':'994000000323','9':'994000000330','10':'994000000347','11':'994000000354','12':'994000000361'},
                             'Black': {'6':'994000000378','7':'994000000385','8':'994000000392','9':'994000000408','10':'994000000415','11':'994000000422','12':'994000000439'}
                             },
       'Classic Flat': {'Black': {'Small':'994000000279','Medium':'994000000286','Large':'994000000293'},
                        'Champagne': {'Small':'994000000248','Medium':'994000000255','Large':'994000000262'},
                        }
       }

def UPCget(St, C, Si):
    return UPC[St][C][Si]

LineNum = raw_input('How many different items are returning? ')
Style = raw_input('Style? C or P: ')
if Style == 'C' or Style == 'c':
    Style = 'Classic Flat'
if Style == 'P' or Style == 'p':
    Style = 'True Premium Flat'
LineNum = int(LineNum)
for num in range(LineNum):
    item = num + 1
    print('\nItem number ' + str(item))
    Color = raw_input('Color: ')
    Size = raw_input('Size: ')
    UPC = UPCget(Style, Color, Size)
    print Color + ', Size ' + Size + ' has UPC code ' + UPC
f.close()

But I keep getting an 'string indices must be integers, not str' error only if my LineNum is greater than 1, and only on the second occurance. 但是,只有当LineNum大于1时,并且仅在第二次出现时,我不断收到“字符串索引必须是整数,而不是str”错误。 I've taken a look at the debugger, but can't seem to find any difference between when I call UPCget the first time VS the second time. 我看过调试器,但似乎找不到第一次调用UPCget与第二次调用UPCget的区别。

Would appreciate some help! 希望能有所帮助!

EDIT: 编辑:

Forgot to post trackback :) 忘记发布引用了:)

How many different items are returning? 2
Style? C or P: P

Item number 1
Color: Silver
Size: 7
Silver, Size 7 has UPC code 994000000521

Item number 2
Color: Champagne
Size: 7

Traceback (most recent call last):
  File "/Users/klhuizinga/Documents/Talaria/ASN/UPCget.py", line 26, in <module>
    UPC = UPCget(Style, Color, Size)
  File "/Users/klhuizinga/Documents/Talaria/ASN/UPCget.py", line 12, in UPCget
    return UPC[St][C][Si]
TypeError: string indices must be integers, not str
UPC = UPCget(Style, Color, Size)

Here's the problem. 这是问题所在。 UPCget works fine in the first iteration of your loop, but then UPC gets overwritten. UPCget在循环的第一次迭代中工作正常,但随后UPC被覆盖。 It's no longer a dict, now it's a string. 不再是字典,现在是字符串。 Then in the second iteration it fails because you can't index a string the way UPCget is doing. 然后在第二次迭代中它失败了,因为您无法像UPCget那样索引字符串。

Try using a different variable name so you don't overwrite the original value. 尝试使用其他变量名,以免覆盖原始值。

code = UPCget(Style, Color, Size)
print Color + ', Size ' + Size + ' has UPC code ' + code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python,Json和字符串索引必须是整数,而不是str - python, Json and string indices must be integers, not str typeerror字符串索引必须是整数,而不是str python - typeerror string indices must be integers not str python TypeError:字符串索引必须是整数,而不是python中的str - TypeError: string indices must be integers, not str in python 编写python脚本以计算每个国家/地区的机场数量,但出现以下错误“字符串索引必须是整数,而不是str” - Writing a python script to counts the number of airports in each country, but getting following error 'string indices must be integers, not str' Python白痴时刻:字符串索引必须是整数,而不是str - Python idiot moment: string indices must be integers, not str Python、Flask - 类型错误:字符串索引索引必须是整数或切片,而不是字符串 - Python, Flask - TypeError: string index indices must be integers or slices, not str Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str TypeError:字符串索引必须是整数,而不是Python字典上的str - TypeError: string indices must be integers, not str on Python Dictionary Scrapy:TypeError:字符串索引必须是整数,而不是str? - Scrapy: TypeError: string indices must be integers, not str? TypeError:字符串索引必须是整数,而不是str-OpenErp - TypeError: string indices must be integers, not str - OpenErp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM