ab= [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650'] ]

Saya ingin mengurai daftar ini ke file CSV dalam format tabel menggunakan python

Ex. :
name              price        description                           Calories
Belgian Waffles   $5.95        Two of our famous Belgian Waffles     650

Catatan: Ukuran daftar dapat bervariasi. Nilainya bisa bervariasi. Tidak ada pengkodean keras yang seharusnya ada di sana.

Xml yang dimaksud adalah

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple 
     syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>Light Belgian waffles covered with an assortment of fresh 
      berries and whipped cream</description>
    <calories>900</calories>
  </food>

Saya mencoba mengekstraknya ke dalam daftar terlebih dahulu dengan makanan sebagai root menggunakan skrip python berikut

def innerHtml(root):
     text = '' 
     nodes = [ root ]
     while not nodes==[]:
        node = nodes.pop()
        if node.nodeType==xml.dom.Node.TEXT_NODE:
            text += node.wholeText
        else:            
            nodes.extend(node.childNodes)          
     return text
innerlist=[]
outerlist=[]
string2=[]

# To get tag value
for statusNode in xmlFile.getElementsByTagName(xmlNode):
    for childNode in statusNode.childNodes:
        if childNode.nodeType==xml.dom.Node.ELEMENT_NODE:
            if innerHtml(childNode).strip() != '':
                string2.append(childNode.nodeName)
                innerlist.append(childNode.nodeName+" 
                "+innerHtml(childNode).strip())
    outerlist.append(innerlist)
    innerlist=[]    
    print (outerlist)

Saya mendapatkan yang berikut sebagai daftar

outerlist = [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650'] , ['name Berry-Berry Belgian Waffles','price $8.95','description Light Belgian waffles covered with an assortment  ','calories 900']]

Saya ingin itu ditulis ke CSV menggunakan python dalam format

name           price       description       calories
<name given>   <price>     <description>     <calories>
-4
Emmanuel 17 Agustus 2017, 16:27

2 jawaban

Jawaban Terbaik
ab= [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650']]
(column_names, row_values) = (list() for i in range(2))

for newlist in range(0,len(ab)):
    for i in range(0,len(ab[newlist])):

        column = ab[newlist][i].split()[0]

        if column not in column_names:
            column_names.append(ab[newlist][i].split()[0])

        row_values.append(re.sub(column_names[i], '', ab[newlist][i]).strip())

df = pd.DataFrame(data=row_values).T
df.columns = column_names
file_name = "yourfilenameandpath"
df.to_csv(file_name, sep='\t', encoding='utf-8')

EDIT:

import pandas as pd
from lxml import etree

xmlfile = archive.open("xmlfile_name.xml")
xmldoc = etree.parse(xmlfile)
root = xmldoc.getroot()

foods  = root.find("breakfast_menu").findall("food")

(name, price, description, calories) = (list() for i in range(4))

for food in foods:
    name.append(food.find("name").text)
    price.append(food.find("price").text)
    description.append(food.find("description").text)
    calories.append(food.find("calories").text)

df= pd.DataFrame({"name": name,
                  "price": price,
                  "description": description,
                  "calories": calories)
df.to_csv(file_name, sep='\t', encoding='utf-8')
1
J. Doe 18 Agustus 2017, 09:46
import csv

ab = [['name Belgian Waffles',
'price $5.95',
'description Two of our famous Belgian Waffles ',
'calories 650'],
['name Chocolate Waffles',
'price $7.95',
'description Two of our famous Chocolate Waffles ',
'calories 1050']]


l = []

for item in ab:
    d = {kv.split()[0]:' '.join(kv.split()[1:]) for kv in item}
    l.append(d)

keys = l[0].keys()

with open("example.csv", 'w') as o:
    writer = csv.DictWriter(o,fieldnames=keys)
    writer.writeheader()
    writer.writerows(l)
0
MetalloyD 17 Agustus 2017, 14:41