I am building an app that relies heavily on plists that are composed of an array of arrays (of strings). I cannot find a tool to convert a csv into such a plist easily. Things I have tried:
An app called csv2plist.py, which infuriatingly only converts the first column into an array and ignores the rest of the csv.
An online tool at mindsizzlers which doesn't seem to be online anymore (at least the connection times out).
An app called 'Plist converter' which only creates a plist that is an array of dictionaries.
Has anyone else been successful with this? Any advice as to how to convert a csv file into a plist that is an array of arrays?
Sample Input (typical csv):
c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,g,g,g,a1,g,g,g,g,c,c,c,c,c,c,c,c,c,c,c,c,c,c
c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,g,g,g,o,g,g,g,g,c,c,c,c,c,c,c,c,c,c,c,c,c,c
c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,g,g,g,o,g,g,g,g,c,c,c,c,c,c,c,c,c,c,c,c,c,c
c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,g,g,g,o,g,g,g,g,c,c,c,c,c,c,c,c,c,c,c,c,c,c
c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,g,g,g,o,g,g,g,g,c,c,c,c,c,c,c,c,c,c,c,c,c,c
Sample output (typical array of arrays plist):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<array>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
</array>
<array>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
<string>f</string>
</array>
Thanks.
I have worked this out. As suggested by a redditer, I modified the open source csv2plist to do what I needed. The result is the following script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import csv
args = sys.argv[1:]
if args:
if(len(args) == 1):
infile = args[0]
outfile = infile.replace(".csv", ".plist")
plisttype = 'array'
if(open(os.path.abspath(outfile),'w')):
if(os.path.isfile(infile)):
data = csv.reader(open(infile))
output = open(os.path.abspath(outfile),'w')
output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
output.write('<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n')
output.write('<plist version="1.0">\n')
output.write('<array>\n')
for row in data:
output.write('<array>\n')
rowList = [elem.strip().split(',') for elem in row]
for i in rowList:
output.write('\t<string>' + ''.join(i) + '</string>\n')
output.write('</array>\n')
output.write('</array>\n')
output.write('</plist>')
output.close()
print os.path.basename(os.path.abspath(outfile)) + ' created successfully'
else:
print infile + ' could not be opened'
exit()
else:
print outfile + ' is not writable'
exit()
else:
print '\ncsv2array usage:\npython csv2array.py <CSVFile>\n';