简体   繁体   English

Python中的子字符串,这是怎么回事?

[英]Substring in Python, what is wrong here?

I'm trying to simulate a substring in Python but I'm getting an error: 我正在尝试在Python中模拟子字符串,但出现错误:

    length_message = len(update)
    if length_message > 140:
        length_url    = len(short['url'])
        count_message = 140 - length_url
        update        = update["msg"][0:count_message] # Substring update variable

    print update
    return 0

The error is the following: 错误如下:

Traceback (most recent call last):
  File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 54, in <module>
    x.updateTwitterStatus({"url": "http://xxx.com/?cat=49s", "msg": "Searching for some ....... tips?fffffffffffffffffffffffffffffdddddddddddddddddddddddddddddssssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedddddddddddddddddddddddddddddddddddddddddddddddfffffffffffffffffffffffffffffffffffffffffffff "})
  File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 35, in updateTwitterStatus
    update        = update["msg"][0:count_message]
TypeError: string indices must be integers

I can't do this? 我做不到

update        = update["msg"][0:count_message]

The variable "count_message" return "120" 变量“ count_message”返回“ 120”

Give me a clue. 给我点暗示。

Best Regards, 最好的祝福,

UPDATE 更新

I make this call, update["msg"] comes from here 我打这个电话,update [“ msg”]来自这里

x = TwitterC()
x.updateTwitterStatus({"url": "http://xxxx.com/?cat=49", "msg": "Searching for some ...... ....?fffffffffffffffffffffffffffffdddddddddddddddddddddddddddddssssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedddddddddddddddddddddddddddddddddddddddddddddddfffffffffffffffffffffffffffffffffffffffffffffddddddddddddddddd"})

Are you looping through this code more than once? 您是否多次遍历此代码?

If so, perhaps the first time through update is a dict, and update["msg"] returns a string. 如果是这样,也许第一次通过update就是字典,并且update["msg"]返回一个字符串。 Fine. 精细。

But you set update equal to the result: 但是您将update设置为等于结果:

update        = update["msg"][0:int(count_message)]

which is (presumably) a string. (大概是)一个字符串。

If you are looping, the next time through the loop you will have an error because now update is a string, not a dict (and therefore update["msg"] no longer makes sense). 如果要循环,则下次执行循环时将出现错误,因为现在update是字符串而不是dict (因此, update["msg"]不再有意义)。


You can debug this by putting in a print statement before the error: 您可以通过在错误之前放入打印语句来调试此错误:

print(type(update))

or, if it is not too large, 或者,如果不是太大,

print(repr(update))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM