简体   繁体   English

使用数据框获取xls数据,并将其发送到API帖子

[英]Get xls data with dataframe, and send it to a API post

I am reading a xls file with pandas, then did some transformation to change the column names and drop some values, and now I need to transform this dataframe to a dictionary or json in the format required by Zendesk and send it via rest 我正在读取带有熊猫的xls文件,然后进行了一些更改以更改列名并删除一些值,现在我需要将此数据帧转换为Zendesk所需格式的字典或json,并通过rest发送

Here is what I already done: 这是我已经做的:

import pandas as pd

def test_loc(df):
    for i in df.index:
        if (df.at[i, 'type'] == 'Apoio'):
            df.at[i, 'type'] = 'Pergunta'


df = pd.read_excel('file.xlsx')
dropList = ['Número', 'Tipo', 'Cliente (Completo)', 'Responsável', 'Cliente: Classificação (Organização)', 'Justificativa', 'Indicador do SLA de Solução']
for i in dropList:
    df.drop(i, inplace=True, axis=1)
df = df.head(3)

df.columns = ['subject', 'created_at', 'type', 'priority', 'status', 'description']
test_loc(df)
df.to_dict()

This is the dataFrame 这是dataFrame

数据帧

And this is the format that i need to build the dict/json 这是我需要构建dict / json的格式

{
  "ticket": {
    "requester_id": 827,
    "assignee_id": 19,
    "subject": "Some subject",
    "description": "A description",
    "created_at": "creation date",
    "status": "the status",
    "priority": "the priority"
    "comments": [
      { "author_id": 827, "value": "This is a comment", "created_at": "2009-06-25T10:15:18Z" },
      { "author_id": 19, "value": "This is a private comment", "public": false }
    ]
  }
}

You are almost there.you know how to do for 1 row. 您几乎快到了。您知道如何进行1行。

now Iterate the steps and keep on appending to the array of object. 现在,重复这些步骤,并继续追加到对象数组。

finally you will get the array of object what you are expecting hope so this will work. 最终,您将获得所期望的对象数组,因此它将起作用。

I found out how to do that. 我发现了该怎么做。

I've created the dict "ticket" first. 我首先创建了字典“票”。 Then transformed the DF to a dictionary using the split parameter. 然后使用split参数将DF转换为字典。 After that I put the DF dictionary data to the ticket dictionary, acessing its indexes. 之后,我将DF字典数据放入票证字典,访问其索引。 I don't know if it is the best way to do. 我不知道这是否是最好的方法。 But it worked. 但这行得通。

di = df.to_dict('split')


ticket['ticket'] = {di['columns'][0]:di['data'][0][0], 
                    di['columns'][1]:di['data'][0][1],
                    di['columns'][2]:di['data'][0][2],
                    di['columns'][3]:di['data'][0][3],
                    di['columns'][4]:di['data'][0][4],
                    di['columns'][5]:di['data'][0][5]}

And it displayed this json 它显示了这个json

{'ticket': {'subject': 'Atendimento - Jéssica - Erro no Código de Barras dos Boletos',
  'created_at': '2018-12-14T16:14:00Z',
  'type': 'question',
  'priority': 'low',
  'status': 'closed',
  'description': 'PROBLEMA:A cliente entrou com problemas na geração dos boletos, o código de barras estava desconfigurado.\xa0SOLUÇÃO:Acessamos a maquina da cliente e instalamos as fontes do windows AZALEIA, após isso ficou correto.',
  'custom_fields': [{'id': 360018333334,
    'value': 'adm__compras_e_licitações'}]}}

Now I am facing a new problem, how to do that with several values? 现在我面临一个新问题,如何用几个值来做到这一点? I did the code abovewith only one row, now I need to iterate through each value, and build a json with several tickets 我只用一行完成了上面的代码,现在我需要遍历每个值,并用几张票构建一个json

As the code below: 如下面的代码:

{'tickets': [{'subject': 'Atendimento - Jéssica - Erro no Código de Barras dos Boletos',
  'created_at': '2018-12-14T16:14:00Z',
  'type': 'question',
  'priority': 'low',
  'status': 'closed',
  'description': 'PROBLEMA:A cliente entrou com problemas na geração dos boletos, o código de barras estava desconfigurado.\xa0SOLUÇÃO:Acessamos a maquina da cliente e instalamos as fontes do windows AZALEIA, após isso ficou correto.',
  'custom_fields': [{'id': 360018333334,
    'value': 'adm__compras_e_licitações'}]}
   {'subject': 'Atendimento - Jéssica - Erro no Código de Barras dos Boletos',
  'created_at': '2018-12-14T16:14:00Z',
  'type': 'question',
  'priority': 'low',
  'status': 'closed',
  'description': 'PROBLEMA:A cliente entrou com problemas na geração dos boletos, o código de barras estava desconfigurado.\xa0SOLUÇÃO:Acessamos a maquina da cliente e instalamos as fontes do windows AZALEIA, após isso ficou correto.',
  'custom_fields': [{'id': 360018333334,
    'value': 'adm__compras_e_licitações'}]
   }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM