【问题标题】:Convert XML to CSV with python使用 python 将 XML 转换为 CSV
【发布时间】:2016-12-21 21:50:27
【问题描述】:

我有以下 XML 文件需要转换为 CSV 才能导入 Magento。在下面 <values> <Value AttributeID="attributes"></Value> 每种产品都有数百种不同的可能性。

我尝试通过命令行使用xml2csvxmlutils.xml2csv,但没有成功。任何帮助,将不胜感激。

<?xml version="1.0" encoding="UTF-8" ?>
<STEP-ProductInformation>
  <Products>
    <Product UserTypeID="Item" ParentID="12345678" AnalyzerResult="included" ID="123456">
      <Name>X8MM</Name>
      <ClassificationReference InheritedFrom="" ClassificationID="" AnalyzerResult=""/>
      <Values>
        <Value AttributeID="Retail Price">46.44</Value>
        <Value AttributeID="Item Class">03017</Value>
        <Value AttributeID="Item Group">03</Value>
        <Value AttributeID="Consumer Description">Super-X 8mm Mauser (8x57) 170 Grain Power-Point</Value>
        <Value AttributeID="Quantity Case">10</Value>
        <Value AttributeID="Bullet Weight">170 gr.</Value>
        <Value AttributeID="Made In The USA">Made In The USA</Value>
        <Value AttributeID="Item Code">X8MM</Value>
        <Value AttributeID="Caliber">8x57 Mauser</Value>
        <Value AttributeID="Catalog Vendor Name">WINCHESTER</Value>
        <Value AttributeID="Quantity per Box">20</Value>
        <Value AttributeID="Item Status">OPEN</Value>
        <Value AttributeID="Wildcat Eligible">Y</Value>
        <Value AttributeID="Item Description">WIN SUPX 8MAU 170 PP 20</Value>
        <Value AttributeID="Primary Vendor">307AM</Value>
        <Value AttributeID="Caliber-Gauge">8X57 MAUSER</Value>
        <Value AttributeID="InventoryTyp">REG</Value>
        <Value AttributeID="Bullet Style">Power-Point</Value>
        <Value AttributeID="ProductPageNumber"/>
        <Value AttributeID="Model Header">8mm Mauser (8x57)</Value>
        <Value AttributeID="Master Model Body Copy">Power Point assures quick and massive knock-down. Strategic notching provides consistent and reliable expansion. Contoured jacket maximum expansion performance. Alloyed lead core increases retained weight for deeper penetration.</Value>
        <Value AttributeID="Master Model Header">Super-X Power-Point</Value>
        <Value AttributeID="Vendor Group">WIN</Value>
      </Values>
      <AssetCrossReference Type="Primary Image" AssetID="WIN_X8MM" AnalyzerResult="included"/>
    </Product>
  </Products>
</STEP-ProductInformation>

【问题讨论】:

  • 要转换成 XML” - 你真的是这个意思吗?
  • 不,将 XML 转换为 csv。

标签: python xml csv magento


【解决方案1】:

我不熟悉“Magento”,但这个程序将您的 XML 文件转换为 CSV 文件。生成的 CSV 文件有一列用于 Name,每个 Value 一列。

from xml.etree import ElementTree as ET
import csv

tree = ET.parse('x.xml')
root = tree.getroot()

columns = ['Name'] + [
    value.attrib.get('AttributeID').encode('utf-8')
    for value in tree.findall('.//Product//Value')]

with open('x.csv', 'w') as ofile:
    ofile = csv.DictWriter(ofile, set(columns))
    ofile.writeheader()
    for product in tree.findall('.//Product'):
        d = {value.attrib.get('AttributeID').encode('utf-8'):
             (value.text or '').encode('utf-8')
             for value in product.findall('.//Values/Value')}
        d['Name'] = product.findtext('Name')
        ofile.writerow(d)

【讨论】:

  • 谢谢@Robᵩ,这似乎是在正确的轨道上。当我用它运行我的主文件时,它确实引发了一个错误。 python convert.py ItemCatDesc2.xml > ItemCatDesc2.csv Traceback(最近一次调用最后一次):文件“convert.py”,第 18 行,在 ofile.writerow(d) 文件“/usr/lib/python2.7/ csv.py",第 152 行,在 writerow 中返回 self.writer.writerow(self._dict_to_list(rowdict)) UnicodeEncodeError:'ascii' 编解码器无法在位置 362 编码字符 u'\xbd':序数不在范围内(128 )
  • Python 2.7 中的 CSV 模块不喜欢 Unicode 字符。试试我最近的编辑。
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多