首页 > 解决方案 > Python在JSON中查找包含在单词/字符串中的单引号

问题描述

我正在将json数据解析为python中的SQL查询,并且需要注意用双引号替换单引号,因为我得到的数据的符号是错误的(我无法改变它)。我遇到的问题是,一些字符串是英文文本并包含单引号。

'comment': 'bla bla it's you're can't bla bla',

我如何只替换书面文本中的那些而不是定义属性的那些?这个正则表达式会是什么样子?

标签: pythonjsonregexparsingescaping

解决方案


虽然我同意您对问题的所有评论,但作为一个练习,我试图从您拥有的内容中获取一个有效的 json 字符串。似乎可以通过涉及字符串操作的几个步骤来完成:

bad = "'comment': 'bla not, really, a comment: bla it's you're can't bla bla'," 
# note that bad has colons, commas and single quotes/apostrophes in it

one = bad.replace("': '",'": "') #separate the key from the value
two = one.replace("'",'"',1) #replace the single quote on the left side of the key with a double quote

#the following lines were lifted from https://stackoverflow.com/a/54945804/9448090
#replace the single quote on the right side of the value with a double quote; drop the last comma:

removal = "'"
reverse_removal = removal[::-1]
replacement = '"'
reverse_replacement = replacement[::-1]

three = two[::-1].replace(reverse_removal, reverse_replacement, 1)[::-1].replace('",','"')
good = "{"+three+"}" #final formatting for json
json.loads(good)

输出:

{'comment': "bla not, really, a comment: bla it's you're can't bla bla"}

推荐阅读