Python:将数据从.XML输入到.CSV文件

时间:2019-06-21 17:46:52

标签: python xml-parsing python-import export-to-csv elementtree

我正在尝试解析以下代码中包含的“ XML”文件,但对于定义的所有变量却报错:

NameError:未定义名称“ computer_name”

这是“ XML”文件的摘录(因为它不是真正的xml文件,因此我试图将变量设置为找到的行下方的行):

        <p1:field>
        <p1:name>NewComputerName</p1:name>
        <p1:value>Computer01</p1:value>
        </p1:field>
        <p1:field>
        <p1:name>NewComputerAssetTag</p1:name>
        <p1:value>12345</p1:value>
        </p1:field>
        <p1:field>
        <p1:name>AcquisitionDate</p1:name>
        <p1:value>4/20/69</p1:value>
        </p1:field>

这是我的代码:

import csv
import os

with open('csvtest.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(('Computer Name', 'Acquisition Date', 'Asset Tag'))
    for filename in os.listdir('\\\\windb\\f$\\Technology\\V1\\0'):
        if filename.endswith(".xml"):
            with open(os.path.join('\\\\windb\\f$\\Technology\\V1\\0',filename), "r") as input:
                for line in input:
                    if line.startswith('    <p1:name>NewComputerName</p1:name>'):
                            computer_name=next(input, '').strip()
                            computer_name=computer_name.split("<p1:value>")[1].split("</")[0]
                    elif line.startswith('    <p1:name>AcquisitionDate'):
                            acqDate=next(input, '').strip()
                            acqDate=acqDate.split("<p1:value>")[1].split("</")[0]
                    elif line.startswith('    <p1:name>NewComputerAssetTag'):
                            assTag=next(input, '').strip()
                            assTag=assTag.split("<p1:value>")[1].split("</")[0]
                myData = [computer_name,acqDate,assTag]
                writer.writerow(myData)

我希望这会将3个变量写入CSV文件,并为目录中的每个XML文件添加一行。

输出为NameError:名称'computer_name'未定义

1 个答案:

答案 0 :(得分:0)

请勿手动解析XML文件,请使用为其创建的库,例如BeautifulSoup:

data = '''<p1:field>
        <p1:name>NewComputerName</p1:name>
        <p1:value>Computer01</p1:value>
        </p1:field>
        <p1:field>
        <p1:name>NewComputerAssetTag</p1:name>
        <p1:value>12345</p1:value>
        </p1:field>
        <p1:field>
        <p1:name>AcquisitionDate</p1:name>
        <p1:value>4/20/69</p1:value>
        </p1:field>'''

from bs4 import BeautifulSoup
import csv

soup = BeautifulSoup(data, 'lxml')
fields = soup.find_all('p1:value')

with open('csvtest.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(('Computer Name', 'Acquisition Date', 'Asset Tag'))

    for n, a, d in zip(fields[::3], fields[1::3], fields[2::3]):
        writer.writerow([n.text, d.text, a.text])

csvtest.csv的内容将是:

Computer Name,Acquisition Date,Asset Tag
Computer01,4/20/69,12345