JSON Object to CSV File Troubleshooting

Sahlusar ahlusar.ahluwalia at gmail.com
Tue Jun 23 07:32:01 EDT 2015


On Tuesday, June 23, 2015 at 2:35:50 AM UTC-4, Denis McMahon wrote:
> On Sun, 21 Jun 2015 16:56:27 -0700, Sahlusar wrote:
> 
> > Here is an example XML document that I am working with:
> 
> > You are welcome to contribute and provide me with feedback. Thank you
> > for your continued feedback and guidance.
> 
> Your XML is invalid! You have a closing MO tag with no opening tag.

Thank you Denis for reiterating this; I am already well aware that my XML document has arbitrarily placed tags.


> 
> > This is the output that I seek:
> 
> > > "a","b"   // headers 
> > > "b",1     // data row 1 
> > > "b",2     // data row 2
> 
> I assume from this that where you have a list of identical elements at 
> the deepest nesting level of the XML file, you require one CSV record for 
> each element in that list, and that you want all higher level element 
> values and attributes duplicated in each CSV record.

Almost, the items in the list should have separate "headers" associated with each tag (and enumerated):

MonthDayCount1, MonthDayCount2, MonthDayCount3, MonthDayCount4 etc....

0                      0              0             0

> 
> I assume that you want to use the tag name of each element as the 
> identifier for the text content of the element (where there is any), and 
> that where an element has no text content, a 0 value is appropriate.

Yes, the tag name is the "key", so to speak. 

> 
> I also assume that as long as the relationship between the headers and 
> the data is correct, it does not matter what order the headers are in, ie 
> the data and headers can both be sorted by the header.
> 
> The output of my code generated once I had corrected your broken XML by 
> inserting an 'MO' opening tag between the 'Response' opening tag and the 
> 'MonthDayCount' opening tag can be seen at:
> 
> http://www.sined.co.uk/tmp/xml_to_csv.txt
> 
> This was generated from xml file:
> 
> http://www.sined.co.uk/tmp/xml_data.txt
> 
> If you want the code that produced it, we can discuss fees, it took a few 
> hours and for consultancy like this I expect a few 10s of $ per hour.


I already have solved this and produced a CSV that meets the clients proposed (yet amorphous requirements). As ChrisA pointed out, programmers should have an intermediate person who could potentially serve as a translator between the client and developer. Unfortunately, this is an ideal that is stifled by labor economics. 

> 
> There may be a generic method to do what you want involving parsing the 
> xml to a nested dictionary / list data object, and then flattening that 
> object, but I don't see that generating you one line of CSV for each 
> Int32 in MonthDayCount.


I have done just that. Thanks again for your help. 

> 
> -- 
> Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list