Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何在JSON中转义撇号?_Python_Json_Pymysql - Fatal编程技术网

Python:如何在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"}] 我试图用''但不起

我有一些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"}]

我试图用
''
但不起作用

使用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