简体   繁体   中英

Unicode decode error when I wanted to read a text file

I have a file that hungarian and I want to modify it by code, but there is problem with it. Here is my simple code:

f = open('nametext','r')

text=f.read()

print(text)

f.close()

the nametext file contains the following strings:

Bevallás iparűzési adófeltöltési kötelezettségről 2013  
Gépjárműadó-kedvezmény mentesség bevallás 2013  
Helyi iparűzési adóbevallás 2013    
Idegenforgalmi adóbevallás 2013 
Kommunális adóbevallás 2013 
Talajterhelési díj bevallás 2013

And I have got an error message, what is the following:

Traceback (most recent call last):

  File "python", line 2, in <module>

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 6: invalid continuation byte

The position 6 is the 7th character int the text what is an hungarian letter: ű

How to compile it? I have compiled the file at repl.it. I have almost the same problem with Ruby. There is a problem with text coding, but I still have not solution.

In your previous question , you stated that the file uses ISO-8859-2 encoding. Thus, you need to specify this encoding in the call to open() :

f = open('nametext', 'r', encoding='iso-8859-2')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM