json.loads(),python 2.7中的\ escape无效

时间:2022-02-06 06:32:27

I am having a list of jsons in a text file as

我在文本文件中有一个jsons列表

[{"key1": "value1", "key2": "value2"},{},{}]

Now I am trying to read the file and converting it into list of dicts so that I can iterate and use those key-value pairs using code -

现在我正在尝试读取文件并将其转换为dicts列表,以便我可以使用代码迭代并使用这些键值对 -

with open('./file.txt') as f:
   listOfDict= json.load(f)

I am getting error -

我收到错误 -

ValueError: Invalid \escape: line 1 column 2005707 (char 2005706)

I think that might be because some of the values are like - For eg. {"key1":"ENERGIZER\xc2"}

我想这可能是因为有些价值观如此 - 例如。 { “键1”: “劲量\ XC2”}

I forgot to use text.encode("utf-8") while writing to this file. Instead I used str(text) while writing to the file.

写入此文件时,我忘记使用text.encode(“utf-8”)。相反,我在写入文件时使用了str(文本)。

Is this is the reason for this error and how can I resolve this issue.

这是导致此错误的原因,我该如何解决此问题。

1 个解决方案

#1


1  

Ok, you'll have to do a little string manipulation to unescape and decode.

好吧,你必须做一些字符串操作来进行unescape和解码。

It depends on whether your data contains escaped Unicodes or escaped 8bit characterset, like latin1 or cp1252. You'll have to experiment to see what works for your data.

这取决于您的数据是否包含转义的Unicodes或转义的8位字符集,如latin1或cp1252。您必须尝试查看哪些内容适合您的数据。

If it's escaped Unicode you can simply do:

如果它是转义Unicode,你可以简单地做:

import io
with io.open('./file.txt', 'r', encoding='unicode_escape') as f:
    listOfDict= json.load(f)

If escaped 8bit 'latin1', you'll need to do:

如果转义为8位'latin1',您需要执行以下操作:

with open('./file.txt', 'r') as f:
    # read, convert escape to byte, convert bytes as 'latin1'
    decoded_json = f.read().decode('string_escape').decode('latin1')
    listOfDict = json.loads(decoded_json) # Note the "s".

#1


1  

Ok, you'll have to do a little string manipulation to unescape and decode.

好吧,你必须做一些字符串操作来进行unescape和解码。

It depends on whether your data contains escaped Unicodes or escaped 8bit characterset, like latin1 or cp1252. You'll have to experiment to see what works for your data.

这取决于您的数据是否包含转义的Unicodes或转义的8位字符集,如latin1或cp1252。您必须尝试查看哪些内容适合您的数据。

If it's escaped Unicode you can simply do:

如果它是转义Unicode,你可以简单地做:

import io
with io.open('./file.txt', 'r', encoding='unicode_escape') as f:
    listOfDict= json.load(f)

If escaped 8bit 'latin1', you'll need to do:

如果转义为8位'latin1',您需要执行以下操作:

with open('./file.txt', 'r') as f:
    # read, convert escape to byte, convert bytes as 'latin1'
    decoded_json = f.read().decode('string_escape').decode('latin1')
    listOfDict = json.loads(decoded_json) # Note the "s".