Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
kindly help with this problem I'm converting csv file to xml but after the covert is done it shown that there is an error cant open the xml file .


python code
------------------------------------------------
Python
csvData = csv.reader(open(csvfil))
    xmlData = open(xmlFile, 'w')
    xmlData.write('<?xml version="1.0"?>' + "\n")
    # there must be only one top-level tag
    xmlData.write('<csv_data>' + "\n")

    rowNum = 0
    for row in csvData:
        if rowNum == 0:
            tags = row
            # replace spaces w/ underscores in tag names
            for i in range(len(tags)):
                tags[i] = tags[i].replace(' ', '_')
        else: 
            xmlData.write('<row>' + "\n")
            for i in range(len(tags)):
                xmlData.write('    ' + '<' + tags[i] + '>' \
                            + row[i] + '</' + tags[i] + '>' + "\n")
            xmlData.write('</row>' + "\n")
                
        rowNum +=1

    xmlData.write('</csv_data>' + "\n")
    xmlData.close()

---------------------------------
xml file
XML
<pre>

<csv_data>
<row>
<recorded>EXPECTED
<3607>3808
<se>VRBSE
<13:25>14:01
<38%>45%
<03:59>05:01
<78%>85%
<04:00>05:01
<33>32
<13:25>14:00
<41>42
<08/01>08/02
<>

<row>
<recorded>EXPECTED
<3607>3508
<se>VRBSE
<13:25>14:00
<38%>30%
<03:59>05:00
<78%>75%
<04:00>05:30
<33>34
<13:25>14:00
<41>46
<08/01>08/03
<>

<row>
<recorded>EXPECTED
<3607>3210
<se>SENW
<13:25>14:00
<38%>30%
<03:59>05:00
<78%>70%
<04:00>05:30
<33>35
<13:25>14:00
<41>49
<08/01>08/04
<>

<row>
<recorded>EXPECTED
<3607>3210
<se>NWSE
<13:25>14:00
<38%>25%
<03:59>05:00
<78%>70%
<04:00>05:30
<33>35
<13:25>14:00
<41>49
<08/01>08/05
<>

<row>
<recorded>Time
<3607>01:00
<se>02:00
<13:25>03:00
<38%>04:00
<03:59>05:00
<78%>06:00
<04:00>07:00
<33>08:00
<13:25>09:00
<41>10:00
<08/01>11:00
<>12:00

<row>
<recorded>Temp
<3607>34
<se>33
<13:25>33
<38%>33
<03:59>33
<78%>33
<04:00>34
<33>36
<13:25>38
<41>39
<08/01>40
<>41

<row>
<recorded>RH%
<3607>65
<se>75
<13:25>75
<38%>77
<03:59>74
<78%>75
<04:00>73
<33>63
<13:25>51
<41>51
<08/01>46
<>44

<row>
<recorded>Time
<3607>13:00
<se>14:00
<13:25>15:00
<38%>16:00
<03:59>17:00
<78%>18:00
<04:00>19:00
<33>20:00
<13:25>21:00
<41>22:00
<08/01>23:00
<>24:00

<row>
<recorded>Temp
<3607>40
<se>40
<13:25>39
<38%>38
<03:59>38
<78%>36
<04:00>35
<33>35
<13:25>34
<41>34
<08/01>34
<>34

<row>
<recorded>RH%
<3607>43
<se>41
<13:25>45
<38%>49
<03:59>50
<78%>52
<04:00>57
<33>59
<13:25>66
<41>65
<08/01>67
<>68



-----------
csv data


RECORDED 3607 SE 13:25 38% 3:59 78% 4:00 33 13:25 41 1-Aug
EXPECTED 3808 VRBSE 14:01 45% 5:01 85% 5:01 32 14:00 42 2-Aug
EXPECTED 3508 VRBSE 14:00 30% 5:00 75% 5:30 34 14:00 46 3-Aug
EXPECTED 3210 SENW 14:00 30% 5:00 70% 5:30 35 14:00 49 4-Aug
EXPECTED 3210 NWSE 14:00 25% 5:00 70% 5:30 35 14:00 49 5-Aug
Time 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00 9:00 10:00 11:00 12:00
Temp 34 33 33 33 33 33 34 36 38 39 40 41 "
"
RH% 65 75 75 77 74 75 73 63 51 51 46 44 %
Time 13:00 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 22:00 23:00 24:00:00
Temp 40 40 39 38 38 36 35 35 34 34 34 34 "
"
RH% 43 41 45 49 50 52 57 59 66 65 67 68 %

What I have tried:

if there is any solution kindly provide it and thnx
Posted
Updated 2-Aug-22 0:54am
Comments
Richard MacCutchan 2-Aug-22 6:55am    
It looks like the first row in the csv data is not a heading line, so all your tag values are incorrect. It is also not clear exactly how you are supposed to structure the XML.

1 solution

Because your XML structure is wrong with more than couple os issues, that's why file is not opening.
<csv_data>
<row>
<recorded>EXPECTED
<3607>3808
<se>VRBSE
<13:25>14:01
<38%>45%
<03:59>05:01
<78%>85%
<04:00>05:01
<33>32
<13:25>14:00
<41>42
<08/01>08/02
<>

Ending Tags missing for csv_data, row and other elements. Text is not enclosed with Tags properly. Tags without any text.

Spend some time on XML
XML Tutorial[^]

Also on Python with XML
Create XML Documents using Python - GeeksforGeeks[^]
Valence Analytics: Python: Converting CSV to XML and JSON[^]

FYI
Common XML errors - Libvirt Wiki[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900