我有这样一个csv文件:
customer, restaurant, drink, dish
1, MC, "C1, C6", "D1, D2, D3"
我想用Python lxml模块得到这样的XML:
<Example>
<Customer> 1 </Customer>
<Restaurant> MC </Restaurant>
<Drink>
<Drink> C1 </Drink>
<Drink> C6 </Drink>
</Drink>
<Dish> D1 </Dish>
<Dish> D2 </Dish>
<Dish> D3 </Dish>
</Example>
我目前尝试的是:
import csv
import lxml.etree
from lxml import etree as ET
from lxml.builder import E
with open(r'restaurant.csv') as csvfile:
result = E.Example(*
( E.Customer(row['customer']),
E.Restaurant([row'restaurant']),
E.Drink(row['drink']),
E.Dish(row['dish']))
for row in csv.DictReader(csv))
)
tree = lxml.etree.tostring(results, pretty_print=True )
with open(r'output.xml', 'wb') as f:
f.write(tree)
我不知道如何在另一个循环中循环这个列表。
1条答案
按热度按时间cvxl0en21#
您可以尝试:
图纸:
data.csv
的含量: