Python: pylightxl:如果用户输入与excel中A行的文本匹配,则将其替换为B行的文本并打印出来



我想用python做一个西班牙语翻译器,所以我用字典创建它。

translate = input("input: ")
translate_dictionary = {
"Hello": "Hola",
"Goodbye": "adiós"
}
for english, spanish in translate_dictionary.items():
translate = translate.replace(english, spanish)
print(translate)

我想用pylightxl重写这个,并链接到excel电子表格,如果用户输入匹配excel A行的文本,将其替换为B中的文本并打印出来,这是我的excel电子表格内容:

<表类="年代桌子">Btbody><你好你好<再见/td>再见

只需遍历excel表行,并将其添加到字典中,如下所示:

translate_dictionary = {}
db = xl.readxl(fn='translate_library.xlsx')
for row in db.ws(ws='Sheet1').rows:
translate_dictionary[row[0]] = row[1]

在你的代码中,看起来像:

import pylightxl as xl
translate_dictionary = {}
db = xl.readxl(fn='translate_library.xlsx')
for row in db.ws(ws='Sheet1').rows:
translate_dictionary[row[0]] = row[1]
translate = input("input: ")
for english, spanish in translate_dictionary.items():
translate = translate.replace(english, spanish)
print(translate)

最新更新