0

I have a csv file with data like

abc1,E,WEL,POI,<DeData L1="Websales" </DeData>

I want to extract individual columns and save into xml file as

<Data>
   <element1>abc1</element1>
   <element2>E</element2>
   <element3>WEL</element3>
   <element4>abc1</element4>
   <DeData L1="Websales" </DeData>
</Data>

and each row from csv file should be saved as separate xml file.

Any pointers would be very helpful.

4
  • So, what is the actual question here? Are you stuck on reading the csv-file, parsing the contents of the file, formatting the output or writing the output to the files? Commented May 22, 2020 at 8:30
  • I'm missing a > in your XML file. Commented May 22, 2020 at 8:30
  • @HampusLarsson - I am looking for code to Parse the content and save formatted output to xml file. Commented May 22, 2020 at 8:50
  • 1
    @Geeme We're not a code-writing service here. Could you please provide the code you've tried that didn't work so that we have something to build on. Commented May 22, 2020 at 8:52

1 Answer 1

1

Try this code.


import pandas as pd

with open('a.csv', 'r') as filee:
  count=0
  temp = filee.readlines()
  for val in temp:
    values = val.rstrip().split(',')
    with open(str(count)+'.xml', 'w') as xml_f:
      string = f'''<Data>
  <element1>{ values[0] }</element1>
  <element2>{ values[1] }</element2>
  <element3>{ values[2] }</element3>
  <element4>{ values[3] }</element4>
  { values[4] }
</Data>'''
      print(string)
      xml_f.write(string.lstrip())
    count += 1

Considering csv file contains data in this format

abc1,E,WEL,POI,<DeData L1="Websales" </DeData>
abc1,E,WEL,POI,<DeData L1="Websales" </DeData>
abc1,E,WEL,POI,<DeData L1="Websales" </DeData>
abc1,E,WEL,POI,<DeData L1="Websales" </DeData>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.