A Python dictionary with repeated fields

2024/11/15 21:51:27

I'm constructing a dictionary with Python to use with a SOAP API.

My SOAP API takes an input like this:

<dataArray><AccountingYearData><Handle><Year>string</Year></Handle><Year>string</Year><FromDate>dateTime</FromDate><ToDate>dateTime</ToDate><IsClosed>boolean</IsClosed></AccountingYearData><AccountingYearData><Handle><Year>string</Year></Handle><Year>string</Year><FromDate>dateTime</FromDate><ToDate>dateTime</ToDate><IsClosed>boolean</IsClosed></AccountingYearData>
</dataArray>

Se this for the full string https://api.e-conomic.com/secure/api1/EconomicWebService.asmx?op=AccountingYear_CreateFromDataArray

Notice how the field appears multiple times.

How can I create a Python dict with this data?

If I do this:

data = {'dataArray':{'AccountingYearData':{'Handle':{'Year':'2017'}, 'Year':'2017','FromDate':'2017-01-01', 'ToDate':'2017-12-31', 'IsClosed':'False'},'AccountingYearData':{'Handle':{'Year':'2017'}, 'Year':'2017','FromDate':'2017-01-01', 'ToDate':'2017-12-31', 'IsClosed':'False'}}
}

I get:

>>> type (data)
<type 'dict'>
>>> data {'dataArray': {'AccountingYearData': {'IsClosed': 'False', 'FromDate': '2017-01-01', 'Handle': {'Year': '2017'}, 'ToDate': '2017-12-31', 'Year': '2017'}}
}

It's as expected I think, but now what I need.

Answer

Well, the answer seems obvious and is even hinted by the "dataArray" name: if you have a list of items, then you want to use a list to store them:

data = {'dataArray':[{'AccountingYearData':{'Handle':{'Year':'2017'}, 'Year':'2017','FromDate':'2017-01-01', 'ToDate':'2017-12-31', 'IsClosed':'False'},},{'AccountingYearData':{'Handle':{'Year':'2017'}, 'Year':'2017','FromDate':'2017-01-01', 'ToDate':'2017-12-31', 'IsClosed':'False'},},]}
https://en.xdnf.cn/q/119011.html

Related Q&A

psexec run python script passed from host

I am trying to run a python script on a remote computer via psexec. I am able to connect and run python.exe with the following:C:\test>psexec \\192.168.X.X -u domain\administrator -p password -i C:…

TypeError: main() missing 1 required positional argument: self

My code and error is below and I was trying to understand why I am getting the error and how to fix it. I tried this without self and got another error TypeError: load_data() takes 0 positional argumen…

Python not calling external program

I am having problems with a python program that I wrote. It is actually plpython3u. I am running the program as a Trigger from postgres. I am pretty sure the trigger part works. My test python prog…

Selenium: How do I retry browser/URL when ValueError(No tables found)

I have a code that scrapes oddsportal website. Sometimes while scraping, I get ValueError("No tables found") and when I manually refresh browser, page loads. How do I do it via code? My code…

For loop for web scraping in python

I have a small project working on web-scraping Google search with a list of keywords. I have built a nested For loop for scraping the search results. The problem is that a for loop for searching keywor…

operation on a variable inside a class in python

Im new with oop and python. Ive been trying to do a simple thing: there is class called Foo(),it contains a variable called x which is initially set to zero.>>>a = Foo() >>>a.x >&g…

Print several sentences with different colors

Im trying to print several sentences with different colors, but it wont work, I only got 2 colors, the normal blue and this redimport sys from colorama import init, AnsiToWin32stream = AnsiToWin32(sys.…

Discord bot to send a random image from the chosen file

I am making a discord bot that randomly chooses an image (images) which is in the same directory (Cats) as the python file(cats.py). This is what my code looks like right now: Cats = os.path.join(os.pa…

pytest - patched method of a class does not return the mock value

My code is fairly simple but i dont understand what is going on :class MyDb :def some_func( arg ) :....while my test code is :@mock.patch(mypkg.mydb) @pytest.mark.parametrize( func_dummy_value ) :( [ {…

New instance of toplevel classes make overlapping widgets

Im generally new to python and tkinter. Ive been programming maybe about a year or so, and Ive just started to try to make each tkinter toplevel window its own class because Ive heard that its the righ…