You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:将无文档电影排期API输出转换为指定字典嵌套列表

解决方案

解析格式说明

原始API输出的每行遵循固定结构:

<Showtime> :- <Channel name> : <Movie name> (<Cast members separated by commas>)

Python转换实现

通过正则匹配提取各字段,代码如下:

import re

# 原始API输出内容
raw_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)"""

# 分割每行并初始化结果列表
lines = raw_output.strip().split('\n')
result = []

# 正则匹配规则,覆盖所有字段
pattern = r'^(\d{2}:\d{2}:\d{2}) :- ([^:]+) : ([^(]+) \((.+)\)$'

for line in lines:
    match = re.match(pattern, line.strip())
    if match:
        # 提取并整理各字段
        showtime = match.group(1)
        channel = match.group(2).strip()
        movie_name = match.group(3).strip()
        cast_list = [name.strip() for name in match.group(4).split(',')]
        
        # 组装字典并加入结果列表
        result.append({
            'Showtime': showtime,
            'Channel name': channel,
            'Movie name': movie_name,
            'cast': cast_list
        })

# 输出转换后的结构
print(result)

最终转换结果

运行代码后得到的嵌套列表结构如下:

[
    {
        'Showtime': '00:00:00',
        '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']
    }
]

补充说明

  • 正则匹配方式比简单字符串分割更稳定,能兼容电影名含特殊字符的场景
  • 演员列表被处理为字符串数组,便于后续业务逻辑调用

内容的提问来源于stack exchange,提问作者KARTIK BISHT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:05:19