gpt4 book ai didi

python - 使用 json 格式化字符串

转载 作者:行者123 更新时间:2023-12-01 02:04:00 24 4
gpt4 key购买 nike

这是一个名为 subtitles.ass 的文件。如果你看一下,你会发现我定义了 {font_size}{sentence[0}。显然我想用我的输入数据来格式化它们。

[Script Info]
; Script generated by FFmpeg/Lavc57.107.100
ScriptType: v4.00+
PlayResX: 384
PlayResY: 288

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: sorry,Arial,{font_size},&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,1,2,10,10,12,1


[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.97,0:00:01.50,sorry,,0,0,0,,{sentence[0]}
Dialogue: 0,0:00:03.11,0:00:04.39,sorry,,0,0,0,,{sentence[1]}
Dialogue: 0,0:00:05.18,0:00:07.26,sorry,,0,0,0,,{sentence[2]}
Dialogue: 0,0:00:07.26,0:00:09.91,sorry,,0,0,0,,{sentence[3]}
Dialogue: 0,0:00:10.00,0:00:11.26,sorry,,0,0,0,,{sentence[4]}
Dialogue: 0,0:00:11.63,0:00:12.70,sorry,,0,0,0,,{sentence[5]}
Dialogue: 0,0:00:13.61,0:00:16.01,sorry,,0,0,0,,{sentence[6]}
Dialogue: 0,0:00:18.08,0:00:19.60,sorry,,0,0,0,,{sentence[7]}
Dialogue: 0,0:00:19.60,0:00:21.60,sorry,,0,0,0,,{sentence[8]}

首先,我从 subtitles.ass 读取数据。

with open(subtitles_file, 'r') as file:
subtitles = file.read()

现在输入数据来了。

{
"font_size": "47",
"sentences": [
....
]
}

最后我想用上面的 json 数据格式化字幕。简单来说,我想将 {font_size} 替换为 47。data 位于 json 之上。

subtitles.format(data.get('font_size'), data.get('sentences'))

但是我遇到了错误。

Traceback (most recent call last): File "iemoji.py", line 17, in subtitles.format(data.get('font_size'), data.get('sentences')) KeyError: 'font_size'

编辑数据

我像这样加载数据。

data = json.load(file)

最佳答案

您需要在 format 函数中解压字典以获取键值对。

尝试:

subtitles.format(**data)

这正是 format_map 的用途。

subtitles.format_map(data)

工作示例:

import json

subtitles = """[Script Info]
; Script generated by FFmpeg/Lavc57.107.100
ScriptType: v4.00+
PlayResX: 384
PlayResY: 288

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: sorry,Arial,{font_size},&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,1,2,10,10,12,1


[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.97,0:00:01.50,sorry,,0,0,0,,{sentence[0]}
Dialogue: 0,0:00:03.11,0:00:04.39,sorry,,0,0,0,,{sentence[1]}
Dialogue: 0,0:00:05.18,0:00:07.26,sorry,,0,0,0,,{sentence[2]}
Dialogue: 0,0:00:07.26,0:00:09.91,sorry,,0,0,0,,{sentence[3]}
Dialogue: 0,0:00:10.00,0:00:11.26,sorry,,0,0,0,,{sentence[4]}
Dialogue: 0,0:00:11.63,0:00:12.70,sorry,,0,0,0,,{sentence[5]}
Dialogue: 0,0:00:13.61,0:00:16.01,sorry,,0,0,0,,{sentence[6]}
Dialogue: 0,0:00:18.08,0:00:19.60,sorry,,0,0,0,,{sentence[7]}
Dialogue: 0,0:00:19.60,0:00:21.60,sorry,,0,0,0,,{sentence[8]}"""

data_str = """{
"font_size": "47",
"sentence": [
"sample sentence0",
"sample sentence1",
"sample sentence2",
"sample sentence3",
"sample sentence4",
"sample sentence5",
"sample sentence6",
"sample sentence7",
"sample sentence8",
"sample sentence9"
]
}"""

data = json.loads(data_str)
print(subtitles.format(**data))#or
#print(subtitles.format_map(data))

关于python - 使用 json 格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com