regex Python如何将单引号转换为双引号以格式化为JSON字符串

hs1ihplo  于 2023-03-24  发布在  Python
关注(0)|答案(5)|浏览(160)

我有一个文件,其中每行都有这样的文本(代表电影的演员):

[{'cast_id': 23, 'character': "Roger 'Verbal' Kint", 'credit_id': '52fe4260c3a36847f8019af7', 'gender': 2, 'id': 1979, 'name': 'Kevin Spacey', 'order': 5, 'profile_path': '/x7wF050iuCASefLLG75s2uDPFUu.jpg'}, {'cast_id': 27, 'character': 'Edie's Finneran', 'credit_id': '52fe4260c3a36847f8019b07', 'gender': 1, 'id': 2179, 'name': 'Suzy Amis', 'order': 6, 'profile_path': '/b1pjkncyLuBtMUmqD1MztD2SG80.jpg'}]

我需要将其转换为有效的json字符串,因此只将必要的单引号转换为双引号(例如,单词Verbal周围的单引号不得转换,文本中的最终撇号也不应转换)。
我使用python 3.x。我需要找到一个正则表达式,它只会将正确的单引号转换为双引号,从而将整个文本转换为有效的json字符串。你有什么想法吗?

uinbv5nw

uinbv5nw1#

首先,你作为例子给出的那行代码是不可解析的!… 'Edie's Finneran' …包含一个语法错误,不管是什么。
假设您可以控制输入,您可以简单地使用eval()来读取文件。(虽然,在这种情况下,人们会想知道为什么您不能首先生成有效的JSON...)

>>> f = open('list.txt', 'r')
>>> s = f.read().strip()
>>> l = eval(s)

>>> import pprint
>>> pprint.pprint(l)
[{'cast_id': 23,
  'character': "Roger 'Verbal' Kint",
  ...
  'profile_path': '/b1pjkncyLuBtMUmqD1MztD2SG80.jpg'}]

>>> import json
>>> json.dumps(l)
'[{"cast_id": 23, "character": "Roger \'Verbal\' Kint", "credit_id": "52fe4260ca36847f8019af7", "gender": 2, "id": 1979, "name": "Kevin Spacey", "order": 5, "rofile_path": "/x7wF050iuCASefLLG75s2uDPFUu.jpg"}, {"cast_id": 27, "character":"Edie\'s Finneran", "credit_id": "52fe4260c3a36847f8019b07", "gender": 1, "id":2179, "name": "Suzy Amis", "order": 6, "profile_path": "/b1pjkncyLuBtMUmqD1MztDSG80.jpg"}]'

如果您无法控制输入,这是非常危险的,因为它为代码注入攻击打开了大门。
最好的解决方案是首先生成有效的JSON,这一点我再怎么强调也不为过。

9gm1akwq

9gm1akwq2#

如果你对JSON数据没有控制权,不要eval()吧!
我创建了一个简单的JSON校正机制,因为它更安全:

def correctSingleQuoteJSON(s):
    rstr = ""
    escaped = False

    for c in s:
    
        if c == "'" and not escaped:
            c = '"' # replace single with double quote
        
        elif c == "'" and escaped:
            rstr = rstr[:-1] # remove escape character before single quotes
        
        elif c == '"':
            c = '\\' + c # escape existing double quotes
   
        escaped = (c == "\\") # check for an escape character
        rstr += c # append the correct json
    
    return rstr

您可以通过以下方式使用该函数:

import json

singleQuoteJson = "[{'cast_id': 23, 'character': 'Roger \\'Verbal\\' Kint', 'credit_id': '52fe4260c3a36847f8019af7', 'gender': 2, 'id': 1979, 'name': 'Kevin Spacey', 'order': 5, 'profile_path': '/x7wF050iuCASefLLG75s2uDPFUu.jpg'}, {'cast_id': 27, 'character': 'Edie\\'s Finneran', 'credit_id': '52fe4260c3a36847f8019b07', 'gender': 1, 'id': 2179, 'name': 'Suzy Amis', 'order': 6, 'profile_path': '/b1pjkncyLuBtMUmqD1MztD2SG80.jpg'}]"

correctJson = correctSingleQuoteJSON(singleQuoteJson)
print(json.loads(correctJson))
mwngjboj

mwngjboj3#

获取所需输出的代码如下

import ast
def getJson(filepath):
    fr = open(filepath, 'r')
    lines = []
    for line in fr.readlines():
        line_split = line.split(",")
        set_line_split = []
        for i in line_split:
            i_split = i.split(":")
            i_set_split = []
            for split_i in i_split:
                set_split_i = ""
                rev = ""
                i = 0
                for ch in split_i:
                    if ch in ['\"','\'']:
                        set_split_i += ch
                        i += 1
                        break
                    else:
                        set_split_i += ch
                        i += 1
                i_rev = (split_i[i:])[::-1]
                state = False
                for ch in i_rev:
                    if ch in ['\"','\''] and state == False:
                        rev += ch
                        state = True
                    elif ch in ['\"','\''] and state == True:
                        rev += ch+"\\"
                    else:
                        rev += ch
                i_rev = rev[::-1]
                set_split_i += i_rev
                i_set_split.append(set_split_i)
            set_line_split.append(":".join(i_set_split))
        line_modified = ",".join(set_line_split)
        lines.append(ast.literal_eval(str(line_modified)))
    return lines
lines = getJson('test.txt')
for i in lines:
    print(i)
t9aqgxwy

t9aqgxwy4#

除了eval()(user3850的回答中提到),还可以使用ast.literal_eval
这已经在线程中讨论过了:Using python's eval() vs. ast.literal_eval()?
您还可以查看以下来自Kaggle竞赛的讨论线程,其中的数据与OP提到的数据类似:
https://www.kaggle.com/c/tmdb-box-office-prediction/discussion/89313#latest-517927https://www.kaggle.com/c/tmdb-box-office-prediction/discussion/80045#latest-518338

vfh0ocws

vfh0ocws5#

import ast
json_dat = json.dumps(ast.literal_eval(row['prod_cat']))
dict_dat = json.loads(json_dat)

相关问题