我正在构建一个依赖于由数组(字符串数组)组成的plists的应用程序。我找不到一个工具来轻松地将csv转换为这样的plist。我尝试过的事情:
一个名为csv2plist.py的应用程序,它只会将第一列转换为数组并忽略其余的csv。
Mindsizzlers上的在线工具似乎不再在线(至少连接超时)。
一款叫做“Plist转换器”的应用程序'它只会创建一个字典数组的plist。
还有其他人成功吗?关于如何将csv文件转换为数组数组的plist的任何建议?
样本输入(典型的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
示例输出(典型的数组数组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>
感谢。
答案 0 :(得分:1)
我已经解决了这个问题。正如redditer所建议的那样,我修改了开源csv2plist以完成我需要的工作。结果是以下脚本:
#!/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';