Here is my code,
from sys import argv
script,errors,encoding =argv
def main(file,encoding,errors):
line = file.readline()
if line:
print_a_line(line,encoding,"ignore")
main(file,encoding,errors)
def print_a_line(line,encoding,errors):
newline=line.strip()
raw_byte=newline.encode(encoding,errors="ignore")
cooked_string=raw_byte.decode(encoding,errors='ignore')
outputfile.write(str(raw_byte))
outputfile.write("<<<<<>>>>>>")
outputfile.write(cooked_string)
language= open("languages.txt",encoding="utf-8")
outputfile=open("test.txt","w")
main(language,encoding,'ignore')
If I use “write” function, it will show errors saying “UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\u12a0’ in position 0: illegal multibyte sequence”(basically I am trying to present the result in a new file instead of presenting them on the terminal)
However, if I just use “print” to replace the last three “write”, the program will run just fine. What’s going on here?Thx