2

我有这个 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

我做了什么试图解决:

首先,我一直在寻找相同问题的答案,但是我找不到任何相似的问题。

根据 sms 变量的打印,看看它返回什么,看看是如何分配 'Location' 的,这是打印上的:

[{'RejectDuplicates': 0, 'SMSCDateTime': datetime.datetime(2014, 7, 1, 16, 31, 58), 'Class': 1, 'Name': u'', 'InboxFolder': 0, 'Text': u'dsgfzggzdfrg', 'SMSC': {'DefaultNumber': u'', 'Format': 'Text', 'Number': u'+#####', 'Validity': 'Max', 'Location': 0, 'Name': u''}, 'ReplaceMessage': 0, 'Coding': 'Default_No_Compression', 'Number': u'+#####', 'DateTime': None, 'DeliveryStatus': 0, 'State': 'UnSent', 'MessageReference': 0, 'Length': 12, 'Location': 0, 'Memory': 'SM', 'ReplyViaSameSMSC': 0, 'UDH': {'Text': '', 'ID16bit': 0, 'AllParts': 0, 'ID8bit': 0, 'PartNumber': -1, 'Type': 'NoUDH'}, 'Type': 'Submit', 'Folder': 2}]

我注意到'Location'等于0,但是你到达那里的关键是'Location'但是现在我陷入了死胡同,当变量本身被制作时,如何将键字符串转换为整数字符串键?我正在使用 gammu,因此返回表单,因为函数的结果是预先格式化的。

有人可以帮我一把吗?

我希望我的信息对您有用,以便您理解我的问题 PS电话号码在###上是故意的

4

2 回答 2

1

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

你能做什么?您可以访问该列表的唯一元素,这将是一个字典:

sms[0]['Location']
......
   '-----> dictionary
于 2014-07-01T16:48:43.427 回答
0

sms是 a list,它不接受类型的索引str

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

它包含一个字典:

>>> type(sms[0])
<type 'dict'>

因此,您可以发出:

>>> sms[0]['Location']
0
于 2014-07-01T16:49:56.320 回答