如何将数据帧转换为常量联系API所需的确切格式?

问题描述 投票:0回答:1

我有一个包含以下数据的数据框:

  email_addresses first_name last_name
0    [email protected]       test      test
1     [email protected]       Jane       Doe
2   [email protected]       John     Smith

我需要将以下格式的数据传递到常量联系人API的'data'参数中。格式需要像这样:

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": "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":"[email protected]","first_name":"test","last_name":"test"},{"\\ufeffemail_addresses":"[email protected]","first_name":"Jane","last_name":"Doe"},{"\\ufeffemail_addresses":"[email protected]","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": ["[email protected]"],
            "first_name": "test",
            "last_name": "test"
        }, {
            "email_addresses": ["[email protected]"],
            "first_name": "Jane",
            "last_name": "Doe"
        }, {
            "email_addresses": ["[email protected]"],
            "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': '[email protected]'},
  {'first_name': 'Jane',
   'last_name': 'Doe',
   '\xef\xbb\xbfemail_addresses': '[email protected]'},
  {'first_name': 'John',
   'last_name': 'Smith',
   '\xef\xbb\xbfemail_addresses': '[email protected]'}],
 'lists': 1072830671}

Aaditi:

跑完线后:

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': '[email protected]',
   u'first_name': u'bob',
   u'last_name': u'bob'},
  {u'email_address': '[email protected]',
   u'first_name': u'tim',
   u'last_name': u'joe'},
  {u'email_address': '[email protected]',
   u'first_name': u'sue1',
   u'last_name': u'sue1'},
  {u'email_address': '[email protected]',
   u'first_name': u'tim3',
   u'last_name': u'test'},
  {u'email_address': '[email protected]',
   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': '[email protected]',
   u'first_name': u'Adam',
   u'last_name': u'Apple'},
  {u'email_address': '[email protected]',
   u'first_name': u'Brenda',
   u'last_name': u'Banana'},
  {u'email_address': '[email protected]',
   u'first_name': u'Charlene',
   u'last_name': u'Cherry'},
  {u'email_address': '[email protected]',
   u'first_name': u'Donna',
   u'last_name': u'Daquiri'}],
 'lists': [1]}

不知道如何放弃'u'

Aadita:

尝试:

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)
json python-3.x pandas api-design
1个回答
1
投票

我认为你需要to_dict字典,最后转换为json

#remove \xef\xbb\xb
#https://github.com/pandas-dev/pandas/issues/4793
df = pd.read_csv(file, encoding="utf-8-sig")
#df.columns = df.columns.str.strip()

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': [{'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': 'John', 
                  'last_name': 'Smith'}],
  'lists': [1]}
© www.soinside.com 2019 - 2024. All rights reserved.