Python “内容”和“文本”有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17011357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between 'content' and 'text'
提问by dotancohen
I am using the terrific Python Requestslibrary. I notice that the fine documentationhas many examples of howto do something without explaining the why. For instance, both r.textand r.contentare shown as examples of howto get the server response. But where is it explained what these properties do?For instance, when would I choose one over the other? I see thar r.textreturns a unicode object sometimes, and I suppose that there would be a difference for a non-text response. But where is all this documented? Note that the linked document does state:
我正在使用了不起的Python Requests库。我注意到精美的文档中有许多示例说明如何做某事而不解释原因。例如,r.text和r.content都显示为如何获取服务器响应的示例。但是哪里解释了这些属性的作用呢?例如,我什么时候会选择一个而不是另一个?我看到 thar有时会r.text返回一个 unicode 对象,我想非文本响应会有所不同。但是,所有这些记录在哪里?请注意,链接的文档确实声明:
You can also access the response body as bytes, for non-text requests:
对于非文本请求,您还可以以字节形式访问响应正文:
But then it goes on to show an example of a text response! I can only suppose that the quote above means to say non-text responsesinstead of non-text requests, as a non-text request does not make sense in HTTP.
但是接下来会显示文本响应的示例!我只能假设上面的引用意味着说non-text responses而不是non-text requests,因为非文本请求在 HTTP 中没有意义。
In short, where is the proper documentationof the library, as opposed to the (excellent) tutorialon the Python Requests site?
简而言之,与 Python Requests 站点上的(优秀)教程相反,该库的正确文档在哪里?
采纳答案by Gary Kerr
The developer interfacehas more details:
该开发接口进行了详细介绍:
r.textis the content of the response in Unicode, and r.contentis the content of the response in bytes.
r.text是响应的 Unicoder.content内容,是响应的内容(以字节为单位)。
回答by PyNEwbie
It seems clear from the documentation is that r.content
从文档中可以清楚地看出 r.content
You can also access the response body as bytes, for non-text requests:
>>> r.content
If you read further down the page it addresses for example an image file
如果您进一步阅读页面,它会解决例如图像文件

