我有一个包含以下数据的数据框:
email_addresses first_name last_name
0 test@test.com test test
1 Jane@Doe.com Jane Doe
2 John@Smith.com John Smith
我需要将以下格式的数据传递到常量联系人API的'data'
参数中。格式必须如下:
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": ["John@Smith.com"],
"first_name": "John",
"last_name": "Smith"
}],
"lists": ["1"]
}
正如您所看到的,我需要在'import_data'
周围用方括号传递字符串lists'
和“'lists_id'
”。
我可以在将数据帧转换为json的同时传递字符串,如下所示:
update_contact = {"import_data" : update_contact.to_json(orient='records'), "lists" : 1}
但输出并不是我想要的。它出来是这样的:
{'import_data': '[{"\\ufeffemail_addresses":"test@test.com","first_name":"test","last_name":"test"},{"\\ufeffemail_addresses":"Jane@Doe.com","first_name":"Jane","last_name":"Doe"},{"\\ufeffemail_addresses":"John@Smith.com","first_name":"John","last_name":"Smith"}]',
'lists': 1}
我需要传递'update_contact'
的API如下所示:
headers = {
'Authorization': 'Bearer x',
'X-Originating-Ip': 'x',
'Content-Type': 'application/json',
}
r = requests.post('https://api.constantcontact.com/v2/activities/addcontacts?api_key=x', headers=headers ,data = update_contact_now)
如何让to_json
向我提供看起来像这样的JSON(对于数据帧中的100行)? :
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": ["John@Smith.com"],
"first_name": "John",
"last_name": "Smith"
}],
"lists": ["1"]
}
编辑:
跑线:
update_contact = {"import_data" : update_contact.to_dict(orient='records'), "lists" : 1}
返回:
{'import_data': [{'first_name': 'test',
'last_name': 'test',
'\xef\xbb\xbfemail_addresses': 'test@test.com'},
{'first_name': 'Jane',
'last_name': 'Doe',
'\xef\xbb\xbfemail_addresses': 'Jane@Doe.com'},
{'first_name': 'John',
'last_name': 'Smith',
'\xef\xbb\xbfemail_addresses': 'John@Smith.com'}],
'lists': 1072830671}
Edit2:
跑完线后:
df['email_addresses'] = df['email_addresses'].apply(lambda x: [x])
update_contact = {"import_data" : df.to_dict(orient='records'), "lists" : [1]}
print (update_contact)
我得到了结果:
{'import_data': [{u'email_address': 'bob.bob19955@gmail.com',
u'first_name': u'bob',
u'last_name': u'bob'},
{u'email_address': 'tim@joe.com',
u'first_name': u'tim',
u'last_name': u'joe'},
{u'email_address': 'sue1@sue1.com',
u'first_name': u'sue1',
u'last_name': u'sue1'},
{u'email_address': 'tim3@test.com',
u'first_name': u'tim3',
u'last_name': u'test'},
{u'email_address': 'paul@ryan.com',
u'first_name': u'Paul ',
u'last_name': u'Ryan'},
{u'email_address': 'TG_FN_121717@TG_LN_121717.com',
u'first_name': u'TG_FN_121717',
u'last_name': u'TG_LN_121717'},
{u'email_address': 'AS_FN_121717@AS_LN_121717.com',
u'first_name': u'AS_FN_121717',
u'last_name': u'AS_LN_121717'},
{u'email_address': 'adamapple000@mailinator.com',
u'first_name': u'Adam',
u'last_name': u'Apple'},
{u'email_address': 'brendabanana000@mailinator.com',
u'first_name': u'Brenda',
u'last_name': u'Banana'},
{u'email_address': 'charlenecherry000@mailinator.com',
u'first_name': u'Charlene',
u'last_name': u'Cherry'},
{u'email_address': 'donnadaquiri000@mailinator.com',
u'first_name': u'Donna',
u'last_name': u'Daquiri'}],
'lists': [1]}
不确定如何放弃'u'
?
Edit3:
尝试:
a = json.dumps(update_contact)
得到了这个错误:
u'[{"error_key":"json.field.invalid","error_message":"#/import_data/0/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/0/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/1/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/1/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/2/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/2/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/3/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/3/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/4/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/4/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/5/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/5/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/6/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/6/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/7/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/7/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/8/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/8/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/9/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/9/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/10/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/10/email_addresses: Property is required but not found."},{"error_key":"json.field.invalid","error_message":"#/import_data/11/email_address: Property was found but is not permitted at this location."},{"error_key":"json.field.missing","error_message":"#/import_data/11/email_addresses: Property is required but not found."}]'
最终编辑:
这些代码行有效:
update_contact.rename(columns={'email_address': 'email_addresses'}, inplace=True)
df['email_addresses'] = df['email_addresses'].apply(lambda x: [x])
update_contact = {"import_data" : df.to_dict(orient='records'), "lists" : [1]}
a = json.dumps(update_contact,ensure_ascii=False)
print (update_contact)
答案 0 :(得分:1)
我认为您需要to_dict
字典并最后转换为 $('#submitCarButton').on('click', function(){
let newCar = new Car( $('#yearIn').val(), $('#makeIn').val(), $( '#modelIn' ).val() );
garage.push(newCar);
console.log(garage);
}); //end enterButton on click
:
json