Python:如何在JSON中转义撇号?
我有一些JSON数据,比如Python:如何在JSON中转义撇号?,python,json,pymysql,Python,Json,Pymysql,我有一些JSON数据,比如“提供的课程”:“经济学、世界历史:调查、妇女历史、其他社会科学课程”} 由于女性历史记录错误: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'s history,Other social science course"}] 我试图用''但不起
“提供的课程”:“经济学、世界历史:调查、妇女历史、其他社会科学课程”}
由于女性历史记录
错误:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'s history,Other social science course"}]
我试图用
''
但不起作用使用ORM或其他方法将数据插入数据库,如查询参数,而不是字符串连接 您尝试过在引号之前添加“\`反斜杠吗?您正在通过字符串连接构建SQL。不要那样做。曾经用户参数。这不是有效的JSON。JSON只使用双引号字符串。@Tomalak成功了@如果你发布了你使用过的相关代码,我会更新投票。这可能会解决即时错误,但不是解决问题的正确方法。决不能从字符串生成SQL。在所有情况下都应使用参数。使用参数时,无需操纵值。
dic = {'Courses Offered': "Economics,World history: survey,Women's history,Other social science course"}
string = dic['Courses Offered']
print(re.sub(r'[\']',"\\'",string))
>>> Economics,World history: survey,Women\'s history,Other social science course