enter image description here您好。我是一个完整的初学者,所以对于你们中的许多人来说,这可能是一个愚蠢的问题,但是仍然 我在终端机(powershell)上的LPTHW上进行第23次练习时运行此代码时遇到问题
from sys import argv
script, econding, error = argv
def main(language_file, encoding, errors):
line = language_file.readline()
if line:
print_line(line, encoding, errors)
return main(language_file, encoding, errors)
def print_line(line, encoding, errors):
next_lang = line.strip()
raw_bytes = next_lang.encode(encoding, errors=errors)
cooked_string = raw_bytes.decode(encoding, errors=errors)
print(raw_bytes, "<====>", cooked_string)
languages = open("languages.txt", encoding = "utf-16")
main(languages, encoding, error)
这就是我在Powershell上获得的东西
python ex23.py utf-16 strict
Traceback (most recent call last):
File "ex23.py", line 22, in <module>
main(languages, encoding, error)
NameError: name 'encoding' is not defined
但是我看不到如何定义模块编码。
提前谢谢!
答案 0 :(得分:1)
该错误消息告诉您,您尚未定义encoding
(即,编译器不知道encoding
所指的是什么)。在这种情况下,这是因为您在此行中有一个错字:
script, econding, error = argv
应为encoding
而不是econding
。