转换整个CSV,而不仅仅是一列

时间:2018-05-21 15:43:24

标签: python csv plist

我有一个小的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>

我在这个剧本中做错了什么?

1 个答案:

答案 0 :(得分:0)

我想我已经解决了。以下脚本似乎产生了我需要的东西:

#!/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';