TypeError:字符串索引必须是整数,而不是Pt[]中的str

2024-09-22 20:18:36 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么会出现这个错误?你知道吗

File Parser.py", line 214, in writeData
comments = subItem['Comments']
TypeError: string indices must be integers, not str

短堆栈跟踪:

213:             for subItem in Pt['C']:
214:                comments = subItem['Comments']

代码:

Pt[] is defined as is a list.
Pt = self.createPatient()

def createPatient(self):
    Pt = {
        'S' : {},
        'C' : []
    }
    return Pt

And 'C' is:

#你知道吗

这是一个关于“C”定义(格式)的较大示例。 格式是列表的字典。这有助于看吗?你知道吗

Formats = {
...
    ['For Future Use', 11, ''],
],
'C' : [
    ['use', 1, ''],
    ['Call', 15, ''],
    ['Leg', 1, '1'],
    ['Rank', 1, 'A'],
    ['DateTime Entered', 14, ''],
    ['User ID', 11, ''],
    ['Comments', 255, ''],
    ['Narrative ID', 11, ''],
    ['For Future Use', 11, ''],
],
'R' : [
    ['Use', 1, ''],
    ['Call #', 15, ''],
    ['Leg', 1, '1'],
....
}

Tags: inselfptidforisuse格式
2条回答

看起来您在代码的前面某处将subItem定义为字符串,而不是字典。检查你的定义

subItem = ....

我猜是一根弦。你知道吗

C的子元素仍然是列表,需要通过它们的索引来访问。 要想做你想做的事,最好的方法是用词汇表代替你在C中的列表。你知道吗

因此,请改用以下方法

{
  'DateTime Entered' : [ 14, ''],
  'User ID' : [11, ''],
  'Comments' : [ 255, '']
}

上面的两行代码就可以了。你知道吗

相关问题 更多 >