简体   繁体   English

将json.dump移植到StringIO代码到python 3

[英]Porting json.dump into StringIO code to python 3

I'm porting a Python 2 application to Python 3. Currently running Python2.7 but, updating code to pass pylint --py3k tests, I have run into a problem with this: 我正在将Python 2应用程序移植到pylint --py3k 。目前正在运行Python2.7,但是,更新代码以通过pylint --py3k测试时,我遇到了以下问题:

def json_resource_file(baseurl, jsondata, resource_info):
    """
    Return a file object that reads out a JSON version of the supplied entity values data. 
    """
    response_file = StringIO()
    json.dump(jsondata, response_file, indent=2, separators=(',', ': '), sort_keys=True)
    response_file.seek(0)
    return response_file

It works fine with: 它可以很好地与:

from StringIO import StringIO

but StringIO.StringIO doesn't exist in Python3 (according to pylint), so using: StringIO.StringIOStringIO.StringIO中不存在(根据pylint),因此使用:

from io import StringIO

I get an error: "TypeError: unicode argument expected, got 'str'" (This is running under Python 2 - I'm still preparing the ground, so to speak, and am not planning to use Python 3 until I have done as much preparation and testing as I can under Python 2.) 我收到一个错误:“ TypeError:需要unicode参数,得到了'str'”(这是在Python 2下运行的-可以这么说,我仍在准备工作,并且不打算使用Python 3,除非我这样做了。在Python 2下可以做很多准备和测试。)

By way of some experimentation, I tried using BytesIO from io , but that gives a different error "TypeError: 'unicode' does not have the buffer interface". 通过一些实验,我尝试使用io BytesIO ,但是给出了另一个错误“ TypeError:'unicode'没有缓冲区接口”。

Clearly, the Python2 version of json.dump is writing str values to the supplied file object. 显然,json.dump的Python2版本正在将str值写入提供的文件对象。 I think the Python3 version of json.dump also writes str (ie Unicode) values. 认为 json.dump的Python3版本也写入了str (即Unicode)值。

So my question is: is there an easy way to dump JSON to a StringIO memory buffer that works with Python 2 and 3? 所以我的问题是:是否有一种简单的方法可以将JSON转储到与Python 2和3兼容的StringIO内存缓冲区?

Notes 笔记

  1. I realize I could use json.dumps and coerce the result to whatever type StringIO expects, but that all seems rather heavy-handed. 我意识到我可以使用json.dumps并将结果强制为StringIO期望的任何类型,但这些似乎都比较笨拙。

  2. I'm also using the following future imports as part of my porting process: 在移植过程中,我还将使用以下将来的导入:

     from __future__ import (unicode_literals, absolute_import, division, print_function) 
  3. Currently, a solution I'm thinking of is to test the python version and import a different version of StringIO accordingly, but that would violate the "Use feature detection instead of version detection" principle. 当前,我正在考虑的解决方案是测试python版本并相应地导入StringIO的其他版本,但这将违反“使用功能检测而不是版本检测”的原则。

  4. In most of my code, using from io import StringIO appears to work fine for situations that I use StringIO . 在我的大多数代码中,使用from io import StringIO在使用StringIO情况下似乎可以正常工作。 It's the specific case of using a StringIO with json.dump that has caused me a few problems. 这是将StringIOjson.dump一起使用的特定情况,这给我带来了一些问题。

If your problem is the import statement, just wrap it in a try statement: 如果您的问题是import语句,则将其包装在try语句中:

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

The rest of your code should work as is, at least the code provided in your question works just fine for me. 您的其余代码应按原样工作,至少您的问题中提供的代码对我来说很好。

使用六的StringIO

from six.moves import StringIO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM