Home > front end >  How to Export 2D Table in a csv file using PyCharm
How to Export 2D Table in a csv file using PyCharm

Time:10-14

I have a xml file: 'product.xml', here is an example of the sample file:

<?xml version="1.0"?>
 <Rowset>
  <ROW>
   <Product_ID>32</Product_ID>
   <Company_ID>2</Company_ID>
   <User_ID>90</User_ID>
   <Product_Type>1</Product_Type>
   <Application_ID>BBC#:1010</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>22</Product_ID>
   <Company_ID>4</Company_ID>
   <User_ID>190</User_ID>
   <Product_Type>2</Product_Type>
   <Application_ID>NBA#:1111</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>63</Product_ID>
   <Company_ID>4</Company_ID>
   <User_ID>99</User_ID>
   <Product_Type>1</Product_Type>
   <Application_ID>BBC#:1212</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>22</Product_ID>
   <Company_ID>2</Company_ID>
   <User_ID>65</User_ID>
   <Product_Type>2</Product_Type>
   <Application_ID>NBA#:2210</Application_ID>
  </ROW>

This is my code:

import xml.etree.cElementTree as ET
tree = ET.parse('product.xml')
root = tree.getroot()

for rows in root:
    for attr in rows:
        if (attr.tag=='User_ID'):
            print('User_ID: '   attr.text)
        if (attr.tag=='Application_ID'):
            print('Application_ID: '   attr.text)

Output for this is:

User_ID: 90
Application_ID: BBC#:1010
User_ID: 190
Application_ID: NBA#:1111
User_ID: 99
Application_ID: BBC#:1212

I am wondering how can I generate a 2D table with Pandas Data frame, using 'Application_ID' and 'User_ID' as ROW Headers and their data as columns, like:

Application_ID    User_ID
BBC#:1010         90     
NBA#:1111         190
BBC#:1212         99

And export these 2D Table results into a csv file to save them, Thank you.

CodePudding user response:

Try:

def parse_row(row):
    ret = {'User_ID':np.nan, 'Application_ID':np.nan}
    for attr in row:
        if attr.tag in ret: ret[attr.tag] = attr.text

    return ret  

out = pd.DataFrame([parse_row(r) for r in root])

Output:

  User_ID Application_ID
0      90      BBC#:1010
1     190      NBA#:1111
2      99      BBC#:1212
3      65      NBA#:2210

CodePudding user response:

Pandas is able to read most file types into DataFrames.

### This line would get you all of your columns
df = pd.read_xml('product.xml')
### Drop (remove) unwanted columns
df.drop(['Product_ID', 'Company_ID', 'Product_Type'], axis=1, inplace=True)
### Export to csv
df.to_csv('outputfile.csv')

CodePudding user response:

Something like the below

import xml.etree.ElementTree as ET
import pandas as pd

xml = '''<?xml version="1.0"?>
 <Rowset>
  <ROW>
   <Product_ID>32</Product_ID>
   <Company_ID>2</Company_ID>
   <User_ID>90</User_ID>
   <Product_Type>1</Product_Type>
   <Application_ID>BBC#:1010</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>22</Product_ID>
   <Company_ID>4</Company_ID>
   <User_ID>190</User_ID>
   <Product_Type>2</Product_Type>
   <Application_ID>NBA#:1111</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>63</Product_ID>
   <Company_ID>4</Company_ID>
   <User_ID>99</User_ID>
   <Product_Type>1</Product_Type>
   <Application_ID>BBC#:1212</Application_ID>
  </ROW>
  <ROW>
   <Product_ID>22</Product_ID>
   <Company_ID>2</Company_ID>
   <User_ID>65</User_ID>
   <Product_Type>2</Product_Type>
   <Application_ID>NBA#:2210</Application_ID>
  </ROW>
  </Rowset>
'''

FIELDS = ['Application_ID','User_ID']
data = []
root = ET.fromstring(xml)
for row in root.findall('.//ROW'):
  data.append([row.find(f).text for f in FIELDS])
df = pd.DataFrame(data,columns=FIELDS)
print(df)

output

  Application_ID User_ID
0      BBC#:1010      90
1      NBA#:1111     190
2      BBC#:1212      99
3      NBA#:2210      65
  • Related