0

我制作了一个使用 argparse 将 csv 文件转换为 xml 文件的程序。首先它将读取 csv 文件作为输入文件,然后将其转换为 xml 文件。这是我的代码:

import sys, argparse
import csv
import indent
from xml.etree.ElementTree import ElementTree, Element, SubElement, Comment, tostring

parser=argparse.ArgumentParser(description='Convert wordlist text files to various formats.', prog='Text Converter')
parser.add_argument('-v','--verbose',action='store_true',dest='verbose',help='Increases messages being printed to stdout')
parser.add_argument('-c','--csv',action='store_true',dest='readcsv',help='Reads CSV file and converts to XML file with same name')
parser.add_argument('-x','--xml',action='store_true',dest='toxml',help='Convert CSV to XML with different name')
parser.add_argument('-i','--inputfile',type=argparse.FileType('r'),dest='inputfile',help='Name of file to be imported',required=True)
parser.add_argument('-o','--outputfile',type=argparse.FileType('w'),dest='outputfile',help='Output file name')
args = parser.parse_args()

def main(argv):
    reader = read_csv(args.inputfile)
    if args.verbose: 
        print ('Verbose Selected')
    if args.toxml:
        if args.verbose:
            print ('Convert to XML Selected')
        generate_xml(reader, args.outputfile)
    if args.readcsv:
        if args.verbose:
            print ('Reading CSV file')
    if not (args.toxml or args.readcsv):
        parser.error('No action requested')
    return 1

def read_csv(inputfile):
      return list(csv.reader(inputfile))

def generate_xml(reader,outfile):
    root = Element('Solution')
    root.set('version','1.0')
    tree = ElementTree(root)

    head = SubElement(root, 'DrillHoles')
    head.set('total_holes', '238')

    description = SubElement(head,'description')
    current_group = None
    i = 0
    for row in reader:
        if i > 0:
            x1,y1,z1,x2,y2,z2,cost = row
            if current_group is None or i != current_group.text:
                current_group = SubElement(description, 'hole',{'hole_id':"%s"%i})

                collar = SubElement (current_group, 'collar',{'':', '.join((x1,y1,z1))}),
                toe = SubElement (current_group, 'toe',{'':', '.join((x2,y2,z2))})                                       
                cost = SubElement(current_group, 'cost',{'':cost})
        i+=1    
    indent.indent(root)
    tree.write(outfile)

if (__name__ == "__main__"):
    sys.exit(main(sys.argv))

然后在我写的命令提示符下, Argparse.py -i 1250_12.csv -o output.xml -x 其中 argparse 是程序名称,1250_12.csv 是 csv 文件名,而 output.xml 是我想要的输出名称-x 是将 csv 转换为 xml 的操作。该程序在 10 分钟前运行,现在出现错误消息:

x1,y1,z1,x2,y2,z2,cost = row
Value error: need more than 1 value to unpack
4

1 回答 1

0

您的 CSV 中似乎有些行不完全是 7 列。也许有一些空行?

您可以使用 atry..except来捕获和处理错误:

def generate_xml(reader,outfile):
    root = Element('Solution')
    root.set('version','1.0')
    tree = ElementTree(root)

    head = SubElement(root, 'DrillHoles')
    head.set('total_holes', '238')

    description = SubElement(head,'description')
    current_group = None
    next(reader) # skip the first line
    for row in reader:
        try:
            x1,y1,z1,x2,y2,z2,cost = row
        except ValueError as err:
            sys.stderr.write('{e}: {r!r}'.format(e=err, r=row))
        if current_group is None or i != current_group.text:
            current_group = SubElement(description, 'hole',{'hole_id':"%s"%i})
            collar = SubElement (current_group, 'collar',{'':', '.join((x1,y1,z1))}),
            toe = SubElement (current_group, 'toe',{'':', '.join((x2,y2,z2))}) 
            cost = SubElement(current_group, 'cost',{'':cost})
    indent.indent(root)
    tree.write(outfile)

此外,如果您设置reader = csv.reader(inputfile)而不是使其成为列表,那么您的程序将需要更少的内存,因为reader它将是迭代器而不是行列表。

此外,要跳过第一行,next(reader),reader必须是迭代器,而不是列表。所以为了上面的工作,还要改变:

reader = read_csv(args.inputfile)

reader = csv.reader(inputfile)
于 2013-06-10T17:53:48.153 回答