【问题标题】:python json dumps unicode errorpython json转储unicode错误
【发布时间】:2016-01-15 17:49:38
【问题描述】:

我正在尝试将字典存储为具有 utf-8 编码的 json 文档,但我似乎做错了什么,无法弄清楚是什么。我已经在下面发布了堆栈跟踪和函数。

def parse_contents(res_dict, file):

content_payload = res_dict['parse']['wikitext']['*']
sections_payload = res_dict['parse']['sections']
db = {}
#parse_captures = ("Owner", "Description", "Usage", "Examples", "Options", "Misc.")

def now_next_iter(iterable):
    import itertools
    a, b = itertools.tee(sections_payload)
    next(b, None)
    return itertools.izip(a, b)

def remove_tags(text):
    import re
    return re.sub('<[^<]+?>', '', text)

for cur, nxt in now_next_iter(sections_payload):

    if cur['toclevel'] == 2:
        head = cur['line']
        db[head] = {}
    elif cur['toclevel']  == 3:
        line = cur['line']
        ibo = cur['byteoffset']
        fbo = nxt['byteoffset']

        content = remove_tags(content_payload[ibo:fbo])
        db[head][line] = content #.encode('utf-8')

with io.open(file, 'w', encoding='utf8') as json_db:
    s = json.dumps( db, sort_keys=True, indent=4,
                    separators=(',', ': '))
    json_db.write(s.encode('utf-8'))

尝试 1:

将打印到文件更改为:

    with io.open(file, 'w', encoding='utf8') as json_db:
    s = json.dumps( db, sort_keys=True, indent=4,
                    ensure_ascii=False, encoding='UTF8', separators=(',', ': '))
    s = s.encode('utf-8')
    json_db.write(s)

输出: 这很令人困惑,因为我认为 s.encode('utf-8') 应该将其更改为 unicode。

【问题讨论】:

    标签: python json unicode


    【解决方案1】:

    您可能需要设置 json.dumps 可选参数 'ensure_ascii=False',和/或在 json.dumps 中设置 encoding='UTF8',而不仅仅是 file.open() 调用,这将允许 json 包使用它的选项来处理非ASCII数据。

    在此处查看文档:https://docs.python.org/2/library/json.html

    【讨论】:

    • 根据您的建议更新了问题,但运气不佳:(
    • nvm,我是个白痴,我在尝试 1 中取出了 s.encode,它成功了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多