【问题标题】:string indices must be integers, not str - Python script字符串索引必须是整数,而不是 str - Python 脚本
【发布时间】:2016-07-18 18:48:09
【问题描述】:

尝试编写一个小脚本,让我在与仓库交互时更轻松。想做一些东西,让我导出一个简单的 csv 文件,其中包含我需要履行的产品的所有不同样式、尺寸、颜色和 UPC 代码。这是处理从我创建的字典中获取 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()

但我不断收到“字符串索引必须是整数,而不是 str”错误,仅当我的 LineNum 大于 1 时,并且仅在第二次出现时。 我查看了调试器,但似乎找不到第一次调用 UPCget 与第二次调用 UPCget 之间的任何区别。

不胜感激!

编辑:

忘记发布引用:)

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

【问题讨论】:

  • 请指出报告错误的行,以及完整的回溯。 return 语句是一个明显的候选者。
  • 什么是完整的回溯?
  • 您可能会考虑不在 UPCget 函数中使用全局变量并将 UPC 字典作为参数传入。
  • 有道理,re:global 变量,谢谢

标签: python string dictionary


【解决方案1】:
UPC = UPCget(Style, Color, Size)

这就是问题所在。 UPCget 在循环的第一次迭代中运行良好,但随后 UPC 被覆盖。它不再是一个字典,现在它是一个字符串。然后在第二次迭代中它失败了,因为你不能像 UPCget 那样索引一个字符串。

尝试使用不同的变量名,以免覆盖原始值。

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

【讨论】:

  • 天哪,我什至没有注意到我将它们命名为相同的东西!多伊!非常感谢,我会把头撞到墙上十亿次。
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
相关资源
最近更新 更多