将API输出转换为字典格式

huangapple go评论68阅读模式
英文:

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",
    },
]

huangapple
  • 本文由 发表于 2023年4月11日 00:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定