trying to convert csv to xml , one xml file should get generated for each 15 records present in csv file
Question:
I am new to python family I am trying to convert csv to xml in my python code,
Answers:
Try reading your input file in chunks and convert each chunk to xml
iteratively. For example,
import pandas as pd
chunksize = 15
def convert_xml(df):
Row_list =[]
# Iterate over each row
for index, rows in df.iterrows():
# Create list for the current row
my_list =[rows.col_1, rows.col_2]
# append the list to the final list
Row_list.append(my_list)
with open('path/to/output/file', 'w') as f:
f.write('n'.join(convert_row(record) for record in Row_list))
for chunk in pd.read_csv('path/to/file', chunksize=chunksize):
convert_xml(chunk)
replace col_1
and col_2
with headers in your csv file.
Cheers!!
If I undesrtand correctly, your code is working correctly but you have only one XML file with last chunk info.
It happens because you loop over chunks and rewrite result.xml
at every iteration.
Just change file name for every chunk:
...
def convert_xml(df, file_name):
...
with open(file_name, 'w') as f:
f.write(...)
idx = 1
for chunk in csv_file:
convert_xml(chunk, f'result_{idx}.xml')
idx += 1
I am new to python family I am trying to convert csv to xml in my python code,
Try reading your input file in chunks and convert each chunk to xml
iteratively. For example,
import pandas as pd
chunksize = 15
def convert_xml(df):
Row_list =[]
# Iterate over each row
for index, rows in df.iterrows():
# Create list for the current row
my_list =[rows.col_1, rows.col_2]
# append the list to the final list
Row_list.append(my_list)
with open('path/to/output/file', 'w') as f:
f.write('n'.join(convert_row(record) for record in Row_list))
for chunk in pd.read_csv('path/to/file', chunksize=chunksize):
convert_xml(chunk)
replace col_1
and col_2
with headers in your csv file.
Cheers!!
If I undesrtand correctly, your code is working correctly but you have only one XML file with last chunk info.
It happens because you loop over chunks and rewrite result.xml
at every iteration.
Just change file name for every chunk:
...
def convert_xml(df, file_name):
...
with open(file_name, 'w') as f:
f.write(...)
idx = 1
for chunk in csv_file:
convert_xml(chunk, f'result_{idx}.xml')
idx += 1