[英]How to replace umlauts in an HTML file?
I'm trying to go through a bunch of html files and replace all the umlauts within with their according html-codes.我正在尝试 go 通过一堆 html 文件,并用相应的 html 代码替换其中的所有变音符号。 For some reason the output of the code shown below is the exactsame as the input and i don't know why.
由于某种原因,下面显示的代码的 output 与输入完全相同,我不知道为什么。
import PySimpleGUI as sg
import pathlib
def ui_input():
file_address = None
layout = [[sg.Text("File: "), sg.InputText(key="-FILE_PATH-"), sg.FileBrowse(file_types=[("Html Files", ".*html")])],
[sg.Submit('Convert'), sg.Cancel('Cancel')]
]
window = sg.Window('Html Umlaut Converter', layout)
event, values = window.read()
if event == 'Cancel':
quit()
elif event == 'Convert':
file_address = values["-FILE_PATH-"]
window.close()
return file_address
def convert():
duden = {"ä": "ä", "Ä": "Ä", "ö": "ö", "Ö": "Ö", "ü": "ü", "Ü": "Ü", "ß": "ß"}
file_address_pre = ui_input()
fap = pathlib.Path(file_address_pre)
file_address_post = fap.parent / (fap.stem + "_converted" + fap.suffix)
for i in duden:
print(i, duden[i])
with open(file_address_pre, 'r') as prefile:
predata = prefile.read()
print("pre: ", predata)
postdata = predata.replace(str(i), str(duden[i]))
print("post: ", postdata)
with open(file_address_post, "w") as postfile:
postfile.write(postdata)
convert()
You are overwriting the output file for every letter in your dict.您正在为字典中的每个字母覆盖 output 文件。 Running that code should replace occurences of ß in the original text, but no others.
运行该代码应该替换原始文本中出现的 ß,但不会替换其他的。 If you want to replace every special letter, try something like this instead:
如果你想替换每个特殊字母,请尝试这样的事情:
def convert():
duden = {"ä": "ä", "Ä": "Ä", "ö": "ö", "Ö": "Ö", "ü": "ü", "Ü": "Ü", "ß": "ß"}
file_address_pre = ui_input()
fap = pathlib.Path(file_address_pre)
file_address_post = fap.parent / (fap.stem + "_converted" + fap.suffix)
with open(file_address_pre, 'r') as prefile:
predata = prefile.read()
print("pre: ", predata)
postdata = predata
for i in duden:
print(i, duden[i])
postdata = postdata.replace(str(i), str(duden[i]))
print("post: ", postdata)
with open(file_address_post, "w") as postfile:
postfile.write(postdata)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.