如何在python中使用ASCII字符(\ u9078)拆分行

时间:2018-03-16 05:23:28

标签: python python-3.x

将属性转换为JSON时,会在ASCII字符中添加额外的反斜杠,如何避免这种情况,请参阅下面的代码

输入文件(sample.properties)

property.key.CHOOSE=\u9078\u629e

代码

import json
def convertPropertiesToJson(fileName, outputFileName, sep='=', comment_char='#'):
    props = {}
    with open(fileName, "r") as f:
        for line in f:
            l = line.strip()
            if l and not l.startswith(comment_char):
                innerProps = {}
                keyValueList = l.split(sep)
                key = keyValueList[0].strip()
                keyList = key.split('.')
                value = sep.join(keyValueList[1:]).strip()
                if keyList[1] not in props:
                    props[keyList[1]] = {}
                innerProps[keyList[2]] = value
                props[keyList[1]].update(innerProps)
    with open(outputFileName, 'w') as outfile:
        json.dump(props, outfile)

convertPropertiesToJson("sample.properties", "sample.json")

输出:(sample.json)

{"key": {"CHOOSE": "\\u9078\\u629e"}}

预期结果:

{"key": {"CHOOSE": "\u9078\u629e"}}

3 个答案:

答案 0 :(得分:2)

问题是输入按原样读取,\u字面上复制为两个字符。最容易解决的问题可能就是:

with open(fileName, "r", encoding='unicode-escape') as f:

这将解码转义的unicode字符。

答案 1 :(得分:0)

问题似乎是您保存了表示为转义字符串的unicode字符。你应该在某些时候解码它们。

更改

l = line.strip()

to(对于Python 2.x)

l = line.strip().decode('unicode-escape')

to(for Python 3.x)

l = line.strip().encode('ascii').decode('unicode-escape')

给出了所需的输出:

{"key": {"CHOOSE": "\u9078\u629e"}}

答案 2 :(得分:0)

我不知道你问题的解决办法,但我发现问题出在哪里。

with open('sample.properties', encoding='utf-8') as f:
    for line in f:
        print(line)
        print(repr(line))
        d = {}
        d['line'] = line
        print(d)

out:
property.key.CHOOSE=\u9078\u629e
'property.key.CHOOSE=\\u9078\\u629e'
{'line': 'property.key.CHOOSE=\\u9078\\u629e'}

我不知道如何添加到字典中添加字符串的repr。