我有一个包含如下数据的 json 文件。我想用双引号字符串(第 2 行)中的空格替换单引号(O'Connor)。谁能帮助我如何使用 python 实现这一目标?我是一个试图理解的初学者。
test.json
{'Location': 'near Roe Hwy (3); Forrestfield; in Perth;', 'Name': "Sam", 'Salary': 1000, 'deptno': 10, 'dname':'sales'}
{'Location': **"near 133 Garling St; O'Connor; in Perth;",** 'Name': "Jam", 'Salary': 3000, 'deptno': 40, 'dname':'sales'}
输出应该是这样的
{'Location': 'near Roe Hwy (3); Forrestfield; in Perth;', 'Name': "Sam", 'Salary': 1000, 'deptno': 10, 'dname':'sales'}
{'Location': **"near 133 Garling St; OConnor; in Perth;"**, 'Name': "Jam", 'Salary': 3000, 'deptno': 40, 'dname':'sales'}
答案 0 :(得分:0)
你可以尝试这样的事情:
text = "\"near 133 Garling St; O'Connor; in Perth;\""
start = text.find("\"")
end = text[start+1:].find("\"")
text[start+1:end].replace('\'','')
在示例中,文本变量是解析为字符串的 json 响应的一部分