GenshinDialog
GenshinDialog copied to clipboard
武器文本提取报错
def process_weapons(self):
"""add story context and 5-level weapon skill descriptions"""
all_readable_files = list(
glob.iglob(os.path.join(self.repo, "Readable", self.lang, "*"))
)
files = [f for f in all_readable_files if "Weapon" in f]
# load story
for weapon_file in files:
idx = int(re.match(".+Weapon(\d+).txt", weapon_file).group(1))
with open(weapon_file, "r", encoding="utf-8") as g:
story = "".join(g.readlines()).strip("\n")
self.map_weaponId_to_info[idx]["ReadableText"] = story
上面这段处理武器过程的时候,
idx = int(re.match(".+Weapon(\d+).txt", weapon_file).group(1))
这一句会报错,因为武器描述文件名称可能有Weapon11513_1.txt; Weapon11513_2.txt
这样的形式(内容也不一样,应该是不同精炼等级的不同描述?)
稍微做了一点修改,以识别同一件武器的不同描述:
for weapon_file in files:
# 使用单个正则表达式匹配两种格式
match = re.match(r".+Weapon(\d+)(?:_(\d))?.txt", weapon_file)
if not match:
continue # 如果文件名不匹配,跳过
idx1 = int(match.group(1))
idx2 = match.group(2) # idx2 可能是 None
# 读取文件内容
with open(weapon_file, "r", encoding="utf-8") as g:
story = "".join(g.readlines()).strip("\n")
# 根据 idx2 的存在与否决定字典键名
readable_text_key = f"ReadableText{idx2}" if idx2 else "ReadableText1"
# 更新字典中的内容
self.map_weaponId_to_info[idx1][readable_text_key] = story