我已成功将pandas数据帧转换为json字符串,同时在开头添加字符串'import_data',如下所示:
a = {"import_data" : df.to_json(orient='records')}
返回:
{'import_data': '[{"\\ufeffemail_addresses":"[email protected]","first_name":"test","last_name":"test","lists":1234567890},{"\\ufeffemail_addresses":"[email protected]","first_name":"Jane","last_name":"Doe","lists":1234567890},{"\\ufeffemail_addresses":"[email protected]","first_name":"Pradeep","last_name":"Patel","lists":1234567890}]'}
但是pandas 'to_json'
,并没有完全转换我需要传递给API的数据帧。
字符串需要,必须如下所示:
update_contact = '{"import_data": [{"email_addresses": ["[email protected]"],"first_name": "test","last_name": "test"},{"email_addresses": ["[email protected]"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["[email protected]"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1234567890"]}'
to_json
在第一个括号周围添加单引号,由于某种原因添加反斜杠,字母'u'和字符串\\ufeff
at email_address
的开头。不知道如何放弃所有这些。
编辑:
这就是我只能将数据传递到API的方法:
headers = {
'Authorization': 'Bearer x',
'X-Originating-Ip': 'x',
'Content-Type': 'application/json',
}
update_contact = '{"import_data": [{"email_addresses": ["[email protected]"],"first_name": "test","last_name": "test"},{"email_addresses": ["[email protected]"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["[email protected]"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1072830671"]}'
r_3 = requests.post('urlapi_key=x', headers=headers ,data = update_contact)
请尝试以下代码。
update_contact = '{\"import_data\": [{\"email_addresses\": [\"[email protected]\"],\"first_name\": \"test\",\"last_name\": \"test\"},{\"email_addresses\": [\"[email protected]\"],\"first_name\": \"Jane\",\"last_name\": \"Doe\"}, {\"email_addresses\": [\"[email protected]\"],\"first_name\": \"Pradeep\",\"last_name\": \"Patel\"}],\"lists\": [\"1234567890\"]}'
我们可以在双引号qazxsw poi之前使用backslash\
来打印它像这样"