2016-08-15 11 views
0

Magentoのインポートには、次のXMLファイルをCSVに変換する必要があります。 <values> <Value AttributeID="attributes"></Value>の下には、製品ごとに異なる数百の可能性があります。PythonでXMLをCSVに変換する

私は、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> 
+0

"** XML ***に変換する" - これは本当に意味ですか? –

+0

いいえ、XMLをcsvに変換します。 – Pop

答えて

1

私は "Magento"に精通していませんが、このプログラムはXMLファイルをCSVファイルに変換します。結果のCSVファイルには、Nameの1つの列と、それぞれValueの1つの列があります。

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) 
+0

正しいトラックにあるような@ Rob Thankありがとうございます。メインファイルを実行したときに1つのエラーが発生しました。 のpython convert.py ItemCatDesc2.xml> ItemCatDesc2.csv トレースバック(最新の呼び出しの最後): ofile.writerow(d)において、 ファイル "convert.py"、ライン18、 ファイル「は/ usr/libに/ UnicodeEncodeError: 'ascii'コーデックは、位置 '362'にu '\ xbd'文字をエンコードできません:序数が範囲外(128) – Pop

+0

Python 2.7のCSVモジュールはUnicode文字を好まない。最近の編集を試してみてください。 –

+0

それは、助けてくれてありがとう。とても有難い! – Pop

関連する問題