我有一个小的python脚本(基于我下载的一个开源脚本),它接受一个输入csv文件,我希望脚本以数组的形式输出一个plist,每行一个数组。这是脚本:
#!/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:
rowList = [elem.strip().split(',') for elem in row]
for i in rowList:
output.write('\t<string>' + i.strip() + '</string>\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';
这是输入csv:
a,a,a
b,b,b
c,c,c
这是现在的输出(不需要):
<?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>
<string>a</string>
<string>b</string>
<string>c</string>
</array>
</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>
<string>a</string>
<string>a</string>
<string>a</string>
</array>
<array>
<string>b</string>
<string>b</string>
<string>b</string>
</array>
<array>
<string>c</string>
<string>c</string>
<string>c</string>
</array>
</plist>
我在这个剧本里做错了什么?