KeyError: 'L194' Python
KeyError: 'L194' Python
这是我正在使用的文件的屏幕截图。它包含 'L194'。
这是完整的一行,只是为了表明拆分后它只有 5 个元素,就像其他句子一样。
L194 +++$+++ u0 +++$+++ m0 +++$+++ BIANCA +++$+++ 我们能快点完成吗? Roxanne Korrine 和 Andrew Barrett 在四人组比赛中遭遇了令人难以置信的可怕 public 分手。再次.
movie_lines = open('movie_lines.txt', mode = 'r', encoding = 'utf-8', errors = 'ignore').read().split('\n')
movie_convo_lines = open('movie_conversations.txt', mode ='r', encoding = 'utf-8', errors='ignore').read().split('\n')
map_line_id_with_text = {}
for line in movie_lines:
extract = line.split('+++$+++')
if len(extract)== 5:
map_line_id_with_text[extract[0]] = extract[4]
list_of_lineids = []
for line in movie_convo_lines[:-1]:
extract = line.split(' +++$+++ ')[-1][1:-1].replace("'","").replace(" ","")
list_of_lineids.append(extract.split(','))
prompts = []
responses = []
for _ in list_of_lineids:
for i in range(len(_)-1):
prompts.append(map_line_id_with_text[_[i]])
responses.append(map_line_id_with_text[_[i+1]])
limit = 0
for i in range (limit, limit+5):
print(prompts[i])
print(responses[i])
当我 运行 这段代码时,我一直收到上面的错误,但我打开的文件确实包含 'L194' 所以我很困惑为什么它不起作用。该错误显示在 prompts.append 命令中。
电影台词的分隔符应该是 ' +++$+++ '
(带有 spaces),您在第二个 for
循环中正确使用了它,但在第一个 for
循环你使用 '+++$+++'
(没有 spaces)作为分隔符,所以在那里提取的行号将有一个尾随 space,导致正确的行号来自 list_of_lineids
在 map_line_id_with_text
.
中找不到
这是我正在使用的文件的屏幕截图。它包含 'L194'。
这是完整的一行,只是为了表明拆分后它只有 5 个元素,就像其他句子一样。
L194 +++$+++ u0 +++$+++ m0 +++$+++ BIANCA +++$+++ 我们能快点完成吗? Roxanne Korrine 和 Andrew Barrett 在四人组比赛中遭遇了令人难以置信的可怕 public 分手。再次.
movie_lines = open('movie_lines.txt', mode = 'r', encoding = 'utf-8', errors = 'ignore').read().split('\n')
movie_convo_lines = open('movie_conversations.txt', mode ='r', encoding = 'utf-8', errors='ignore').read().split('\n')
map_line_id_with_text = {}
for line in movie_lines:
extract = line.split('+++$+++')
if len(extract)== 5:
map_line_id_with_text[extract[0]] = extract[4]
list_of_lineids = []
for line in movie_convo_lines[:-1]:
extract = line.split(' +++$+++ ')[-1][1:-1].replace("'","").replace(" ","")
list_of_lineids.append(extract.split(','))
prompts = []
responses = []
for _ in list_of_lineids:
for i in range(len(_)-1):
prompts.append(map_line_id_with_text[_[i]])
responses.append(map_line_id_with_text[_[i+1]])
limit = 0
for i in range (limit, limit+5):
print(prompts[i])
print(responses[i])
当我 运行 这段代码时,我一直收到上面的错误,但我打开的文件确实包含 'L194' 所以我很困惑为什么它不起作用。该错误显示在 prompts.append 命令中。
电影台词的分隔符应该是 ' +++$+++ '
(带有 spaces),您在第二个 for
循环中正确使用了它,但在第一个 for
循环你使用 '+++$+++'
(没有 spaces)作为分隔符,所以在那里提取的行号将有一个尾随 space,导致正确的行号来自 list_of_lineids
在 map_line_id_with_text
.