Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试按索引访问列表时出现Python错误-“;列表索引必须是整数,而不是str”;_Python_Gammu - Fatal编程技术网

尝试按索引访问列表时出现Python错误-“;列表索引必须是整数,而不是str”;

尝试按索引访问列表时出现Python错误-“;列表索引必须是整数,而不是str”;,python,gammu,Python,Gammu,我有以下Python代码: sm.GetSMSStatus() sm.GetSMSFolders() sms = sm.GetNextSMS(Start = True, Folder=0) sms = sm.GetNextSMS(Location = sms['Location'], Folder=0) sms = sm.GetSMS(Location = loc, Folder = 0) 我得到了一个错误: Traceback (most recent call last):

我有以下Python代码:

sm.GetSMSStatus() 
sm.GetSMSFolders() 
sms = sm.GetNextSMS(Start = True, Folder=0)
sms = sm.GetNextSMS(Location = sms['Location'], Folder=0)
sms = sm.GetSMS(Location = loc, Folder = 0)
我得到了一个错误:

    Traceback (most recent call last):
  File "sms_teste.py", line 43, in <module>
    sms = sm.GetNextSMS(Location = sms['Location'], Folder=0)
TypeError: list indices must be integers, not str
我注意到'Location'等于0,但你到达那里的关键是'Location' 但现在我陷入了死胡同,当变量本身生成时,如何将键字符串转换为整数​​用字符串键? 我使用的是gammu,因此函数的结果是预先格式化的,因此会返回表单

有人能帮我一下吗

我希望我的信息能对你有所帮助,以便你能理解我的问题
另外,电话号码是故意在####上的

发生的事情是,

sms
是一个列表(因为括号中有
[]
)。它唯一的元素是字典
{}

您可以做什么?您可以访问该列表的唯一元素,即字典:

sms[0]['Location']
......
   '-----> dictionary
>>> type(sms[0])
<type 'dict'>

sms
是一个
列表
,它不接受类型为
str
的索引:

>>> type(sms)
<type 'list'>

所以
sms
是一个包含一本词典的列表,而不是一本词典<代码>短信['Location]'失败。啊,好的!那么,每当您想要访问一个列表元素时,它就好像是在json中工作一样?感谢您的帮助这与json无关,使用
some_list[index]
是访问列表/元组特定索引中元素的语法。我知道,这是python,json谈到了我们可以访问json传递的部分数组的方式:)
>>> type(sms[0])
<type 'dict'>
>>> sms[0]['Location']
0