如何从带有未转义反斜杠的JSON文件读取? [英] How to read from a JSON file with unescaped backslashes?

查看:309
本文介绍了如何从带有未转义反斜杠的JSON文件读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含连接字符串的JSON文件:

I have a JSON file that contains a connection string:

abc.json

{
  "host":"1.2.3.4",
  "user":"abc",
  "passwd":"s&]\yz$&u42/",
  "dbname":"sample",
  "port":2341
}

这是我的Python脚本尝试读取数据:

Here's my Python script which tries to read the data:

 import psycopg2 as pg
 dbconn = "C:\abc.json"
 with open(dbconn) as conn_file:
     conn = json.load(conn_file)

它给了我这个错误:

json.decoder.JSONDecodeError: Invalid \escape: line 4 column 16 (char 53)

如何解决此错误?

推荐答案

您的文件无效:没有这样的转义序列,例如 \y 在JSON中,并且必须自己转义反斜杠,因此: \\

Your file is not valid: there's no such escape sequence as \y in JSON, and bare backslashes must themselves be escaped thus: \\.

如果可以的话,最简单的解决方案是修复

The simplest solution, if you can, is to fix your file so that it is valid JSON, by escaping that backslash.

如果由于某种原因您不能这样做,则可以为 json.loads()捕获此特定错误并修补源文本:

If for some reason you can't, it's possible to write a wrapper for json.loads() that catches this specific error and patches up the source text:

import json
from json.decoder import JSONDecodeError

def permissive_json_loads(text):
    while True:
        try:
            data = json.loads(text)
        except JSONDecodeError as exc:
            if exc.msg == 'Invalid \\escape':
                text = text[:exc.pos] + '\\' + text[exc.pos:]
            else:
                raise
        else:
            return data

为简单起见,上面的函数使用字符串而不是文件。

For the sake of simplicity, the function above takes a string rather than a file.

这也是一种大锤,可以破解坚果方法,反复尝试加载整个JSON文档,并将所有未转义的反斜杠修复为它找到了它们-对于很少出现问题的小型JSON文档是合理的,但是如果您要处理带有许多这些未转义的反斜杠错误的大型JSON文档,则不合适。

It's also something of a "sledgehammer to crack a nut" approach, repeatedly trying to load the whole JSON document and fixing any unescaped backslashes as it finds them – which is reasonable for small JSON documents that rarely have the problem, but less appropriate if you're handling large JSON documents with lots of these unescaped backslash errors.

在这里起作用:

>>> print(text)
{
  "host":"1.2.3.4",
  "user":"abc",
  "passwd":"s&]\yz$&u42/",
  "dbname":"sample",
  "port":2341
}

>>> config = permissive_json_loads(text)
>>> print(config['passwd'])
s&]\yz$&u42/

以您为例,您将从文件中读取一个字符串,然后在该字符串上调用该函数:

In your case, you'd read from your file into a string, and call the function on that:

 dbconn = "C:\abc.json"
 with open(dbconn) as conn_file:
     conn_doc = conn_file.read()
 conn = permissive_json_loads(conn_doc)

这篇关于如何从带有未转义反斜杠的JSON文件读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆