我已经成功地将pandas数据帧转换为json字符串,同时在开头添加字符串“import\u data”,如下所示:
a = {"import_data" : df.to_json(orient='records')}
这将返回:
{'import_data': '[{"\\ufeffemail_addresses":"test@test.com","first_name":"test","last_name":"test","lists":1234567890},{"\\ufeffemail_addresses":"Jane@Doe.com","first_name":"Jane","last_name":"Doe","lists":1234567890},{"\\ufeffemail_addresses":"Pradeep@Patel.com","first_name":"Pradeep","last_name":"Patel","lists":1234567890}]'}
然而,'to_json'
转换数据帧并不是我需要传递到API中的东西。你知道吗
字符串需要如下所示:
update_contact = '{"import_data": [{"email_addresses": ["test@test.com"],"first_name": "test","last_name": "test"},{"email_addresses": ["Jane@Doe.com"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["Pradeep@Patel.com"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1234567890"]}'
to_json
是在第一个括号周围添加单引号,出于某种原因添加反斜杠,字母'u',以及在email_address
开头添加字符串\\ufeff
。我不知道该怎么放下这些。你知道吗
编辑:
这就是我只能将数据传递到API的方式:
headers = {
'Authorization': 'Bearer x',
'X-Originating-Ip': 'x',
'Content-Type': 'application/json',
}
update_contact = '{"import_data": [{"email_addresses": ["test@test.com"],"first_name": "test","last_name": "test"},{"email_addresses": ["Jane@Doe.com"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["Pradeep@Patel.com"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1072830671"]}'
r_3 = requests.post('urlapi_key=x', headers=headers ,data = update_contact)
请尝试以下代码。你知道吗
我们可以在双引号
"
之前使用反斜杠\
这样打印\"
相关问题 更多 >
编程相关推荐