将 csv 转换为数组数组的 Plist

Convert csv to Plist of Array of Arrays

我正在构建一个严重依赖于由数组(字符串)数组组成的 plist 的应用程序。我找不到可以轻松将 csv 转换为此类 plist 的工具。我尝试过的事情:

  1. 一个名为 csv2plist.py 的应用程序,它只将第一列转换为数组而忽略了 csv 的其余部分。

  2. mindsizzlers 的一个在线工具,现在好像不在线了(至少连接超时了)。

  3. 一个名为 'Plist converter' 的应用程序,它只创建一个 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>

谢谢。

我已经解决了。根据 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';