使用json格式化字符串

2024-06-07 17:05:37 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一个名为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数据格式化字幕。简单地说,我想用47替换{font\u size}。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编辑

我像这样加载数据。你知道吗

data = json.load(file)

Tags: 数据jsonformatdatasizegetscriptsentences
2条回答

这里的错误不是来自data.get,而是来自font_size。你知道吗

^{}字符串中使用命名参数时,必须提供具有匹配名称的关键字值。但是你只提供位置值,所以没有匹配的。你知道吗

你可以通过做一些笨拙的事情来解决这个问题,比如:

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

但实际上,这正是^{}的目的:

subtitles.format_map(data)

作为将来的参考,如果(暂时)分解表达式,则调试类似的内容会容易得多,因此可以确切地看到导致异常的部分:

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

然后你会看到是第三行,而不是第一行,它提高了KeyError,并且更容易找出原因。你知道吗


最后,我不知道你为什么用data.get('font_size')而不是仅仅用data['font_size']。如果你这样做是为了得到一个默认值而不是一个KeyError如果你在数据dict中不考虑字体大小,你几乎肯定不希望这个默认值是None,是吗?无论如何,如果有一个很好的理由,甚至可以简化:把data变成collections.defaultdict(lambda: whatever-default-value-you-want),那么任何缺少的键都会有这个默认值。(即使与format_map一起使用。)

format函数中解压字典以获得键值对所需的内容。你知道吗

尝试:

subtitles.format(**data)

这正是地图的格式。你知道吗

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))

相关问题 更多 >

    热门问题