专家!
我正在尝试使用 Twython 制作一个 Twitter 客户端。现在,我从交互式解释器中检索命令。
在上传图片的测试中,我传递了这个字符串:
"tweet photo: 'tweeted from python test' + path: 'C:\Users\akhya_000\Pictures\My Pictures\Bing.png"
但我发现它无效并导致此错误:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 51-52: truncated \UXXXXXXXX escape
有人可以帮我看看这个字符串有什么问题吗?我应该做什么来修复它?
最佳答案
您想要转义反斜杠或使用原始字符串,因为 Python 会看到 \U
并将其解释为 Unicode escape sequence .
转义看起来像这样:
"tweet photo: 'tweeted from python test' + path: 'C:\\Users\\akhya_000\\Pictures\\My Pictures\\Bing.png"
一个raw string ,其中 Python 将忽略转义序列,如下所示:
r"tweet photo: 'tweeted from python test' + path: 'C:\Users\akhya_000\Pictures\My Pictures\Bing.png"
关于python - Unicode 转义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210458/