You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本无法将完整CSV转为嵌套数组形式plist,请求排查问题

问题分析:CSV转嵌套数组PLIST脚本的错误修正

你提到基于开源代码修改的Python脚本,目标是将CSV每行转为PLIST中的一个子数组,但当前输出没达到预期。咱们先梳理下相关内容:

输入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. 错误的行处理逻辑:csv.reader本身就会把每行拆分成元素列表,你额外加的elem.strip().split(',')完全多余——这会把本已拆分好的行元素再次拆分,导致逻辑混乱。
  2. 缺失子数组包裹:原脚本没有为每行数据创建独立的<array>标签,所有元素都直接放到最外层数组里,自然没法形成“每行对应一个子数组”的结构。
  3. 冗余的文件判断:if(open(os.path.abspath(outfile),'w'))这种写法不规范,而且没必要,用with语句自动管理文件句柄更稳妥。

修复后的脚本

#!/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")
        
        if os.path.isfile(infile):
            try:
                # 用with语句自动管理文件,避免手动关闭遗漏
                with open(os.path.abspath(outfile), 'w') as output, open(infile, 'r') as csv_file:
                    data = csv.reader(csv_file)
                    # 写入PLIST头部
                    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')
                    
                    # 遍历每行,生成独立子数组
                    for row in data:
                        # 跳过空行(可选处理)
                        if not row:
                            continue
                        # 写入子数组开始标签
                        output.write('    <array>\n')
                        # 写入当前行的每个元素为string
                        for elem in row:
                            clean_elem = elem.strip()
                            output.write('        <string>{}</string>\n'.format(clean_elem))
                        # 写入子数组结束标签
                        output.write('    </array>\n')
                    
                    # 写入PLIST尾部
                    output.write('</plist>')
                
                print(f'{os.path.basename(os.path.abspath(outfile))} created successfully')
            except IOError as e:
                print(f'Error writing file: {e}')
        else:
            print(f'{infile} does not exist or is not a file')
            sys.exit()
    else:
        print('csv2array usage:\npython csv2array.py <CSVFile>')
else:
    print('csv2array usage:\npython csv2array.py <CSVFile>')

关键修改点说明

  1. 正确处理CSV行:直接使用csv.reader返回的每行列表,不再多余拆分,确保每行元素被正确识别。
  2. 添加子数组结构:对每行数据,先写<array>开始标签,写完该行所有<string>元素后再写</array>,实现每行对应一个独立子数组。
  3. 优化文件操作:用with语句自动管理文件句柄,添加异常捕获处理IO错误,让脚本更健壮。
  4. 代码可读性优化:调整缩进和换行,用format方法拼接字符串,避免冗余的字符串拼接。

内容的提问来源于stack exchange,提问作者zeeple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:29:49