开发者

Substring in Python, what is wrong here?

开发者 https://www.devze.com 2023-02-20 15:37 出处:网络
I\'m trying to simulate a substring in Python but I\'m getting an error: length_message = len(update) if length_message > 140:

I'm trying to simulate a substring in Python but I'm getting an error:

    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"

Give me a clue.

Best Regards,

UPDATE

I make this call, update["msg"] comes from here

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. Fine.

But you set update equal to the result:

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).


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号