Skip to content

Instantly share code, notes, and snippets.

@davetromp
Created February 24, 2013 22:47
Show Gist options
  • Save davetromp/5026086 to your computer and use it in GitHub Desktop.
Save davetromp/5026086 to your computer and use it in GitHub Desktop.
# This python script will loop through a csv file and put the first column in # the id field and the second in the description field of a localization file. # The csv file needs to be UTF-8 encoded. Delimiter is a comma, no quotes please.
import sys
# DT 24-2-2013
# This python script will loop through a csv file and put the first column in
# in the id field and the second in the description field of a localization file.
# The csv file needs to be UTF-8 encoded. Delimiter is a comma, no quotes please.
#write the standaard output to a file
sys.stdout = open('output.xml', 'w')
# write xml beginning
sys.stdout.write('<?xml version="1.0" encoding="utf-8" standalone="yes"?><language>'),
#loop through file line by line
# open the file for reading
fopen = open('input.csv', 'r')
#read the opened file line by line
for line in fopen:
# split the line string by the delimiter, which is a comma
# each part will be stored as an item of a list ('water','fire')
linelist = line.split(',')
first = linelist[0]
second = linelist[1]
# build the string witht the data from the list
outdata ="""<t id="%s"><nl>%s</nl></t>""" %(first, second)
# write out the new string to stdout
sys.stdout.write(outdata)
# close the file again once we are done
fopen.close()
# write xml ending
sys.stdout.write('</language>'),
# close the output file
sys.stdout.close()
ENENEN Encyclopedieën ; Encyclopedieën
ENPEPE Personen ; Personen
SNSNSN Snelinfo ; Snelinfo
SNADAD Adresgidsen ; Adresgidsen
SNKAKA Kaarten ; Kaarten
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<language>
<t id="ENENEN">
<nl>Encyclopedieën ; Encyclopedieën</nl>
</t>
<t id="ENPEPE">
<nl>Personen ; Personen</nl>
</t>
<t id="SNSNSN">
<nl>Snelinfo ; Snelinfo</nl>
</t>
<t id="SNADAD">
<nl>Adresgidsen ; Adresgidsen</nl>
</t>
<t id="SNKAKA">
<nl>Kaarten ; Kaarten</nl>
</t>
</language>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment