2

Edit2/8 1PM: fixed format of the code, added more detail to better understand what I am asking because I wrote this in a rush. I am making a file converter in python and for some reason it only takes the last line of the example file I made and it adds brackets "['']" I don't want in it probably because it's collected data? I don't know how to make the loop so it collects the data in each row/column and spits it back out while also keeping the csv header parts in.

info = pd.read_csv(filename)
f = open(filename)
csv_f = csv.reader(f)
header=[]
data=[]
things=[]

n = -2
m = 0
i = -1


for col in info.columns:
    header.append(col)
    print(header)
    
for row in csv_f:
    data.append(row)
    for things in data[1:]:
        print(things)
    
num = len(things)
file = open('output.xml', 'w')

while num > 0:
    n+=1
    m+=1
    i+=1
    num -= 1
    
    print("""<%s>""" % (header[i:m]))
    print("""%s""" % (things[i:m]))
    print("""</%s>""" % (header[i:m]))

    if num > 0:
        file.write("""<%s>%s</%s>""" %(header[i:m], things[i:m], header[i:m]))
    else:
        file.close

1 Answers1

0

I tried to rewrite your code, you can add print, to see the results. I was inspired by this answer

import csv
import pandas as pd

f = open(filename)
csv_f = csv.reader(f)
info = pd.read_csv(filename)


header= list(info.columns)
#print(header)


data = []

for row in csv_f: 
   data.append(row)
#print(data)



def convert_row(row):
     str_row = """<%s>%s</%s>"""*len(header)
     var_values = [list_of_elments[k] for k in range(len(header)) for list_of_elments in [header,row,header]]
     var_values =tuple(var_values)
     return str_row % var_values

#print('\n'.join([convert_row(row) for row in data[1:]]))
with open('output.xml', 'w') as myfile: 
  myfile.write('\n'.join([convert_row(row) for row in data]))

For example for :

this example

I have got this result

result

I'm not sure if it's the expected result as I'm not familiar with the xml format.

Edit 1 :

I had to add a head and tail, so I added "<data>" and "</data>" (you can call it something else). I also made a small change on the function convert_row (to convert row by row to xml).

import csv

import pandas as pd
f = open(filename)
csv_f = csv.reader(f)


info = pd.read_csv(filename)
print(info)

header= list(info.columns)


data = []

for row in csv_f: 
   data.append(row)



def convert_row(row):
     str_row = """<%s>%s</%s> \n"""*(len(header)-1)
     str_row = """<%s>%s""" +"\n"+ str_row + """</%s>"""
     var_values = [list_of_elments[k] for k in range(1,len(header)) for list_of_elments in [header,row,header]]
     var_values = [header[0],row[0]]+var_values+[header[0]]
     var_values =tuple(var_values)
     return str_row % var_values

#text ="<data>"+"\n"+'\n'.join([convert_row(row) for row in data[1:]])+"\n" +"</data>"
#print(text)
with open('output.xml', 'w') as myfile: 
  myfile.write("<data>"+"\n"+'\n'.join([convert_row(row) for row in data[1:]])+"\n" +"</data>")

Now you will be able to get an output.xml file and open it.

Edit 2 :

A simpler solution is to use pandas directly with df.to_xml(), for example :

info = pd.read_csv(filename)

#print(info.to_xml())
with open('outputf.xml', 'w') as myfile: 
  myfile.write(info.to_xml())
Anass
  • 396
  • 2
  • 10
  • Thanks. Everything here was great and helpful. I didn't know about Edit 2 and I wish I knew about that shorter way of doing it sooner. lol I spent so much time overthinking I guess. xD – Jannessa Markovich Feb 11 '22 at 18:27
  • For most file convestions in python, there is probably a library that does it for you. You will just need to look for it. – Anass Feb 12 '22 at 01:16