python - 将 Json 转换为 XML - 带有数组值的 Json
问题描述
我知道有很多像我这样的类似问题,但没有一个对我有用。我的 json 文件有演员、导演和流派的数组。如果在构建 xml 时这个数组,我很难处理。
这是json文件:
[
{
"title":"The Kissing Booth",
"year":"2018",
"actors":[
"Megan du Plessis",
"Lincoln Pearson",
"Caitlyn de Abrue",
"Jack Fokkens",
"Stephen Jennings",
"Chloe Williams",
"Michael Miccoli",
"Juliet Blacher",
"Jesse Rowan-Goldberg",
"Chase Dallas",
"Joey King",
"Joel Courtney",
"Jacob Elordi",
"Carson White",
"Hilton Pelser"
],
"genre":[
"Comedy",
"Romance"
],
"description":"A high school student is forced to confront her secret crush at a kissing booth.",
"directors":[
"Vince Marcello"
]
},
{
"title":"Dune",
"year":"2020",
"actors":[
"Rebecca Ferguson",
"Zendaya",
"Jason Momoa",
"Timoth\u00e9e Chalamet",
"Dave Bautista",
"Josh Brolin",
"Oscar Isaac",
"Stellan Skarsg\u00e5rd",
"Javier Bardem",
"Charlotte Rampling",
"David Dastmalchian",
"Stephen McKinley Henderson",
"Sharon Duncan-Brewster",
"Chen Chang",
"Babs Olusanmokun"
],
"genre":[
"Adventure",
"Drama",
"Sci-Fi"
],
"description":"Feature adaptation of Frank Herbert's science fiction novel, about the son of a noble family entrusted with the protection of the most valuable asset and most vital element in the galaxy.",
"directors":[
"Denis Villeneuve"
]
},
{
"title":"Parasite",
"year":"2019",
"actors":[
"Kang-ho Song",
"Sun-kyun Lee",
"Yeo-jeong Jo",
"Woo-sik Choi",
"So-dam Park",
"Jeong-eun Lee",
"Hye-jin Jang",
"Myeong-hoon Park",
"Ji-so Jung",
"Hyun-jun Jung",
"Keun-rok Park",
"Jeong Esuz",
"Jo Jae-Myeong",
"Ik-han Jung",
"Kim Gyu Baek"
],
"genre":[
"Comedy",
"Drama",
"Thriller"
],
"description":"Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.",
"directors":[
"Bong Joon Ho"
]
},
{
"title":"Money Heist",
"year":null,
"actors":[
"\u00darsula Corber\u00f3",
"\u00c1lvaro Morte",
"Itziar Itu\u00f1o",
"Pedro Alonso",
"Miguel Herr\u00e1n",
"Jaime Lorente",
"Esther Acebo",
"Enrique Arce",
"Darko Peric",
"Alba Flores",
"Fernando Soto",
"Mario de la Rosa",
"Juan Fern\u00e1ndez",
"Rocco Narva",
"Paco Tous",
"Kiti M\u00e1nver",
"Hovik Keuchkerian",
"Rodrigo De la Serna",
"Najwa Nimri",
"Luka Peros",
"Roberto Garcia",
"Mar\u00eda Pedraza",
"Fernando Cayo",
"Antonio Cuellar Rodriguez",
"Anna Gras",
"Aitana Rinab Perez",
"Olalla Hern\u00e1ndez",
"Carlos Su\u00e1rez",
"Mari Carmen S\u00e1nchez",
"Antonio Romero",
"Pep Munn\u00e9"
],
"genre":[
"Action",
"Crime",
"Mystery",
"Thriller"
],
"description":"An unusual group of robbers attempt to carry out the most perfect robbery in Spanish history - stealing 2.4 billion euros from the Royal Mint of Spain."
},
{
"title":"The Vampire Diaries",
"year":null,
"actors":[
"Paul Wesley",
"Ian Somerhalder",
"Kat Graham",
"Candice King",
"Zach Roerig",
"Michael Trevino",
"Nina Dobrev",
"Steven R. McQueen",
"Matthew Davis",
"Michael Malarkey"
],
"genre":[
"Drama",
"Fantasy",
"Horror",
"Mystery",
"Romance",
"Thriller"
],
"description":"The lives, loves, dangers and disasters in the town, Mystic Falls, Virginia. Creatures of unspeakable horror lurk beneath this town as a teenage girl is suddenly torn between two vampire brothers."
}
]
我想将我的 json 文件转换为 xml,我的 python 代码:
import json as j
import xml.etree.ElementTree as ET
with open("imdb_movie_sample.json") as json_format_file:
data = j.load(json_format_file)
root = ET.Element("movie")
ET.SubElement(root,"title").text = data["title"]
ET.SubElement(root,"year").text = str(data["year"])
actors = ET.SubElement(root,"actors") #.text = data["actors"]
actors.text = ''
for i in jsondata[0]['movie'][0]['actors']:
actors.text = actors.text + '\n\t\t' + i
genre = ET.SubElement(root,"genre") #.text = data["genre"]
genre.text = ''
for i in jsondata[0]['movie'][0]['genre']:
genre.text = genre.text + '\n\t\t' + i
ET.SubElement(root,"description").text = data["description"]
directors = ET.SubElement(root,"directors") #.text = data["directors"]
directors.text = ''
for i in jsondata[0]['movie'][0]['directors']:
directors.text = directors.text + '\n\t\t' + i
tree = ET.ElementTree(root)
tree.write("imdb_sample.xml")
有谁知道如何帮助我做到这一点?谢谢。
解决方案
我在 pypi 上找到了这个。在询问其他人之前,我总是会尝试查看 pypi 以查看存在的内容。它是一个很棒的资源,包含由大量开发人员创建的 python 包。
推荐阅读
- c++ - 矢量之间的性能差距
和数组 - javascript - 了解日期和时间到特定时区的转换
- javascript - 如何在 setTimeOut() 函数处理时显示加载程序
- mysql - MySQL“插入位置”失败
- javascript - 延迟样式组件中的css动画不透明度
- android - 在android上使用状态时反应本机textInput的错误
- azure - 是否有 Powershell/Azure-CLI 甚至是门户选项来复制公式以在 Azure 中创建 VM?
- mysql - 将连接数组传递给laravel中的查询
- arrays - 如何有效地将 unsigned char* 传递给 C++/CLI 中的托管方法?
- laravel-5 - 从 laravel 将用户添加到 phpbb3 论坛用户表