简体   繁体   中英

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

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

数据帧

And this is the format that i need to build the 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.

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. After that I put the DF dictionary data to the ticket dictionary, acessing its indexes. 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

{'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

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'}]
   }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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