英文:
Converting an api output to dictionary format
问题
[
{
"Showtime": "00:00:00",
"Channel": "MN+ HD",
"Movie": "Gone Girl",
"Cast": [
"Ben Affleck",
"Rosamund Pike",
"Neil Patrick Harris",
"Tyler Perry",
"Carrie Coon",
"Kim Dickens",
"Patrick Fugit",
"Missi Pyle",
"Casey Wilson",
"Sela Ward",
"David Clennon",
"Leonard Kelly Young",
"Darin Cooper"
]
},
{
"Showtime": "00:00:00",
"Channel": "MNX",
"Movie": "Double Impact",
"Cast": [
"Jean Claude Damme",
"Geoffrey Lewis",
"Alan Scarfe",
"Bolo Yeung",
"Alonna Shaw",
"Alicia Stevenson",
"Peter Malota"
]
}
]
英文:
I am working on an app that takes in Schedule of movies airing across different channels in my country as an Api output . The problem is I can't make heads or tails of the output provided by the developer . He hasn't provided any documentation and example output result.
output = 00:00:00 :- MN+ HD : Gone Girl (Ben Affleck, Rosamund Pike, Neil Patrick Harris, Tyler Perry, Carrie Coon, Kim Dickens, Patrick Fugit, Missi Pyle, Casey Wilson, Sela Ward, David Clennon, Leonard Kelly Young, Darin Cooper)
00:00:00 :- MNX : Double Impact (Jean Claude Damme, Geoffrey Lewis, Alan Scarfe, Bolo Yeung, Alonna Shaw, Alicia Stevenson, Peter Malota)
I want to convert this into a nested list of lists with each list containing a dictionary with key/value pairs being Showtime , Channel name , Movie name and cast .
Thank you
答案1
得分: 2
[
{
"Channel Name": "MN+ HD",
"Movie Name": "Gone Girl",
"Cast": [
"Ben Affleck",
"Rosamund Pike",
"Neil Patrick Harris",
"Tyler Perry",
"Carrie Coon",
"Kim Dickens",
"Patrick Fugit",
"Missi Pyle",
"Casey Wilson",
"Sela Ward",
"David Clennon",
"Leonard Kelly Young",
"Darin Cooper"
],
"Showtime": "00:00:00"
},
{
"Channel Name": "MNX",
"Movie Name": "Double Impact",
"Cast": [
"Jean Claude Damme",
"Geoffrey Lewis",
"Alan Scarfe",
"Bolo Yeung",
"Alonna Shaw",
"Alicia Stevenson",
"Peter Malota"
],
"Showtime": "00:00:00"
}
]
英文:
You can use re
module for the task:
import re
output = '''\
00:00:00 :- MN+ HD : Gone Girl (Ben Affleck, Rosamund Pike, Neil Patrick Harris, Tyler Perry, Carrie Coon, Kim Dickens, Patrick Fugit, Missi Pyle, Casey Wilson, Sela Ward, David Clennon, Leonard Kelly Young, Darin Cooper)
00:00:00 :- MNX : Double Impact (Jean Claude Damme, Geoffrey Lewis, Alan Scarfe, Bolo Yeung, Alonna Shaw, Alicia Stevenson, Peter Malota)'''
for a, b, c in re.findall(r':-\s*(.*?)\s*:\s*(.*?)\s*\((.*?)\)', output):
print(f'channel name: {a}')
print(f'movie name: {b}')
cast = [n.strip() for n in c.split(',')]
print(f'cast: {cast}')
print('-' * 80)
Prints:
channel name: MN+ HD
movie name: Gone Girl
cast: ['Ben Affleck', 'Rosamund Pike', 'Neil Patrick Harris', 'Tyler Perry', 'Carrie Coon', 'Kim Dickens', 'Patrick Fugit', 'Missi Pyle', 'Casey Wilson', 'Sela Ward', 'David Clennon', 'Leonard Kelly Young', 'Darin Cooper']
--------------------------------------------------------------------------------
channel name: MNX
movie name: Double Impact
cast: ['Jean Claude Damme', 'Geoffrey Lewis', 'Alan Scarfe', 'Bolo Yeung', 'Alonna Shaw', 'Alicia Stevenson', 'Peter Malota']
--------------------------------------------------------------------------------
A list of dictionaries as an output:
import re
output = """\
00:00:00 :- MN+ HD : Gone Girl (Ben Affleck, Rosamund Pike, Neil Patrick Harris, Tyler Perry, Carrie Coon, Kim Dickens, Patrick Fugit, Missi Pyle, Casey Wilson, Sela Ward, David Clennon, Leonard Kelly Young, Darin Cooper)
00:00:00 :- MNX : Double Impact (Jean Claude Damme, Geoffrey Lewis, Alan Scarfe, Bolo Yeung, Alonna Shaw, Alicia Stevenson, Peter Malota)"""
out = []
for showtime, a, b, c in re.findall(
r"^(.*?)\s*:-\s*(.*?)\s*:\s*(.*?)\s*\((.*?)\)", output, flags=re.M
):
cast = [n.strip() for n in c.split(",")]
out.append({"Channel Name": a, "Movie Name": b, "Cast": cast, "Showtime": showtime})
print(out)
Prints:
[
{
"Channel Name": "MN+ HD",
"Movie Name": "Gone Girl",
"Cast": [
"Ben Affleck",
"Rosamund Pike",
"Neil Patrick Harris",
"Tyler Perry",
"Carrie Coon",
"Kim Dickens",
"Patrick Fugit",
"Missi Pyle",
"Casey Wilson",
"Sela Ward",
"David Clennon",
"Leonard Kelly Young",
"Darin Cooper",
],
"Showtime": "00:00:00",
},
{
"Channel Name": "MNX",
"Movie Name": "Double Impact",
"Cast": [
"Jean Claude Damme",
"Geoffrey Lewis",
"Alan Scarfe",
"Bolo Yeung",
"Alonna Shaw",
"Alicia Stevenson",
"Peter Malota",
],
"Showtime": "00:00:00",
},
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论