7

I am tryng to conenct to a SOAP webservice and use pandas to put in on a table.

Zeep give me this list:

[{
    'ssPeca': '103',
    'ssQtd': '1',
    'ssUn': 'un'
}, {
    'ssPeca': '291A',
    'ssQtd': '8',
    'ssUn': 'un'
}, {
    'ssPeca': '406B',
    'ssQtd': '8',
    'ssUn': 'un'
}]

my code is this:

client = zeep.Client(wsdl=wsdl)
pecas=client.service.TabelaPecas("C-160","CR")

pd.DataFrame.from_dict(pecas)

and that code generate this:

      0     1    2
0  ssPeca ssQtd ssUn 
1  ssPeca ssQtd ssUn 
2  ssPeca ssQtd ssUn 

but i want this:

      0     1    2
0    103    1   un 
1    291A   8   un 
2    406B   8   un 

can anyone help? i am just a beginner in python.

Joao barreira
  • 91
  • 1
  • 6
  • What's the type of `pecas`? If it were a dict, you would get the desired output. Try `pd.DataFrame.from_records(pecas)` maybe? Or try `pecas = dict(pecas)` first. – ayhan Jan 30 '18 at 18:34
  • `pd.DataFrame(pecas)` should do the job – Orenshi Jan 30 '18 at 18:53
  • Hi @Orenshi , pd.DataFrame(pecas) gives the same result. – Joao barreira Jan 31 '18 at 11:31
  • Hi @ayhan, pecas is a List. pd.DataFrame.from_records(pecas) does not Work. pecas = dict(pecas) gives me this error: "dictionary update sequence element #0 has length 3; 2 is required" – Joao barreira Jan 31 '18 at 11:32
  • I deleted my answer since I'm not quite understanding what `pecas` contains. Does `pecas` equal the list you first mentioned in the post? – Orenshi Jan 31 '18 at 13:03
  • @Orenshi, I did a connection to a soap webservice and it give me that response in a form of a list that i put in the main post. When i do a print(pecas) i get that list. – Joao barreira Jan 31 '18 at 13:13

3 Answers3

13

Zeep has a function to transform the response into python objects. Eg: Ordered Dict.

You should use:

from zeep.helpers import serialize_object

client = zeep.Client(wsdl=wsdl)
pecas=client.service.TabelaPecas("C-160","CR")

pecas = serialize_object(pecas)

pd.DataFrame(pecas)

source: http://docs.python-zeep.org/en/latest/helpers.ht

edit: Fixed typo, thanks voglster

1

It works like this:

from zeep import Client
import pandas as pd

labels = ['Peça', 'Qtd']
desenho="C-160"
montagem="CR"

client = Client('xxxxxxxxxxxxxxxxxxxxxxxx')
resposta=list(client.service.TabelaPecas(desenho,montagem))

pecas=[]
for record in resposta:
    peca = record.ssPeca
    qtd  = record.ssQtd
    pecas.append([peca,qtd])


print('Desenho', desenho ,' Montagem', montagem)
pd.DataFrame(data=pecas,columns=labels ) 

Probably it is because i am a beginner in python and probably this is a workaround but works with the zeep response. Using pd.DataFrame with a list works but not with zeep response.

Maybe some day will help a beginner like me.

Joao barreira
  • 91
  • 1
  • 6
0

You can pass the data (list of dicts) directly to the pd.DataFrame. And it should look the way you want it.

>>> data = [{
...     'ssPeca': '103',
...     'ssQtd': '1',
...     'ssUn': 'un'
... }, {
...     'ssPeca': '291A',
...     'ssQtd': '8',
...     'ssUn': 'un'
... }, {
...     'ssPeca': '406B',
...     'ssQtd': '8',
...     'ssUn': 'un'
... }]
>>> pd.DataFrame(data=data)
  ssPeca ssQtd ssUn
0    103     1   un
1   291A     8   un
2   406B     8   un
Orenshi
  • 1,773
  • 11
  • 12