【发布时间】:2026-01-05 18:35:01
【问题描述】:
我正在编写一个相当简单的代码,将欧洲葡萄牙语输入转换为巴西葡萄牙语——因此有很多重音字符,例如 á、é、À、ç 等。
基本上,目标是从一个列表中找到文本中的单词,并将它们替换为第二个列表中的 BR 单词。
代码如下:
#-*- coding: latin-1 -*-
listapt=["gestão","utilizador","telemóvel"]
listabr=["gerenciamento", "usuário", "celular"]
while True:
#this is all because I need to be able to input multiple lines of text, seems to be working fine
print ("Insert text")
lines = []
while True:
line = raw_input()
if line != "FIM":
lines.append(line)
else:
break
text = '\n'.join(lines)
for word in listapt:
if word in text:
num = listapt.index(word)
wordbr = listabr[num]
print(word + " --> " + wordbr) #just to show what changes were made
text = text.replace(word, wordbr)
print(text)
我使用 IDLE 并双击 .py 文件在 Windows 上运行代码。
该代码在使用 IDLE 时运行良好,但在双击 .py 文件时无法匹配和替换字符。
【问题讨论】:
-
如果在某处添加基本的
print "gestão",是否会收到相同的错误消息? -
不,我不知道。这似乎工作正常。
-
我用一个新问题编辑了这个问题——它可以通过 IDLE 工作,但是当我直接运行或转换为 exe 时它不会。为什么?
-
“转换为exe”是什么意思?
-
我使用 py2exe 创建了一个可执行文件,只是为了测试它,因为它可能会成为这个的最终目标。
标签: python windows python-2.7 character-encoding