【问题标题】:Python requests unicode errorPython 请求 unicode 错误
【发布时间】:2012-06-26 15:40:21
【问题描述】:

我正在使用一个简单的 Python 脚本将文件 POST 到 PHP 脚本:

...
url = "http://example.com/upload.php"
r = requests.post(url, data=data, files=files)
...

我需要捕获响应文本,它存储在

r.text

但是当响应包含 ASCII 字符(例如图像文件)时,Python 会失败并出现以下错误:

content = str(self.content, encoding, errors='replace')
TypeError: unicode() argument 2 must be string, not None

有什么办法可以避免这个错误?

【问题讨论】:

  • 没有将None 作为第二个参数(似乎encoding)? (即,了解enconding 的来源可能很有用)
  • 发布更多信息,也许是调用堆栈?

标签: python character-encoding python-requests


【解决方案1】:

图像等二进制文件没有关联的字符编码。使用r.content 而不是r.text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多