Home > other >  Loop is only taking last line of data from the file and adding more characters?
Loop is only taking last line of data from the file and adding more characters?

Time:02-10

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

CodePudding user response:

I tried to rewrite your code, you can add print, to see the results. I was inspired by this 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())
  • Related