简体   繁体   English

Python3 错误:initial_value 必须是 str 或 None,带有 StringIO

[英]Python3 error: initial_value must be str or None, with StringIO

While porting code from python2 to 3 , I get this error when reading from a URL将代码从python2移植到3 ,从 URL 读取时出现此错误

TypeError: initial_value must be str or None, not bytes.类型错误:initial_value 必须是 str 或 None,而不是字节。

import urllib
import json
import gzip
from urllib.parse import urlencode
from urllib.request import Request


service_url = 'https://babelfy.io/v1/disambiguate'
text = 'BabelNet is both a multilingual encyclopedic dictionary and a semantic network'
lang = 'EN'
Key  = 'KEY'

    params = {
        'text' : text,
        'key'  : Key,
        'lang' :'EN'

        }

url = service_url + '?' + urllib.urlencode(params)
request = Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib.request.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
            buf = StringIO(response.read())
            f = gzip.GzipFile(fileobj=buf)
            data = json.loads(f.read())

The exception is thrown at this line在这一行抛出异常

buf = StringIO(response.read())  

If I use python2, it works fine.如果我使用 python2,它工作正常。

response.read() returns an instance of bytes while StringIO is an in-memory stream for text only. response.read()返回一个bytes实例,而StringIO是仅用于文本的内存中流。 Use BytesIO instead.改用BytesIO

From What's new in Python 3.0 - Text Vs.来自Python 3.0 的新增功能 - 文本 Vs。 Data Instead Of Unicode Vs. 数据而不是 Unicode Vs。 8-bit 8 位

The StringIO and cStringIO modules are gone. StringIOcStringIO模块消失了。 Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.相反,导入io模块并分别将io.StringIOio.BytesIO用于文本和数据。

This looks like another python3 bytes vs. str problem.这看起来像是另一个 python3 bytes vs. str问题。 Your response is of type bytes (which is different in python 3 from str ).您的响应是bytes类型(在 python 3 中与str )。 You need to get it into a string first using response.read().decode('utf-8') say and then use StringIO on it.您需要先使用response.read().decode('utf-8') say 将其转换为字符串,然后在其上使用StringIO Or you may want to use BytesIO as someone said - but if you expect it to be str , preferred way is to decode into an str first.或者您可能BytesIO有人说的那样使用BytesIO - 但如果您希望它是str ,首选方法是先decodestr

考虑使用 Six.StringIO 而不是 io.StringIO。

暂无
暂无

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

相关问题 StringIO initial_value必须为str,而不是Bytes - StringIO initial_value must be str, not Bytes TypeError:initial_value必须是str还是none,而不是python 3中的字节? - TypeError: initial_value must be str or none, not bytes in python 3? TypeError:initial_value必须是unicode或None,而不是str, - TypeError: initial_value must be unicode or None, not str, TypeError:initial_value必须为str或None,而不是字节(bigquery) - TypeError: initial_value must be str or None, not bytes (bigquery) 在Windows平台上,dateutil.parser.parse()给出错误“ initial_value必须为unicode或None,不是str” - dateutil.parser.parse() gives error “initial_value must be unicode or None, not str” on Windows platform 使用IMAP提取标头错误TypeError:initial_value必须为str或None,而不是字节 - Using IMAP to extract headers error TypeError: initial_value must be str or None, not bytes Python3 类型错误:“值”必须是 str 或字节的实例,而不是元组 - Python3 TypeError: 'value' must be an instance of str or bytes, not a tuple 期望在python3中抛出错误为“必须在str中,而不是字节” - expect in python3 is throwing error as “must be in str , not bytes” 在 Python3 中解码字节字符串时出错 [TypeError: must be str, not bytes] - Error decoding byte string in Python3 [TypeError: must be str, not bytes] Python 错误:InsertError:replace() 参数 2 必须是 str,而不是 None - Python Error: InsertError: replace() argument 2 must be str, not None
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM