Zoltán Fodor
Zoltán Fodor

Reputation: 123

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.

Upvotes: 0

Views: 253

Answers (1)

jwodder
jwodder

Reputation: 57460

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')

Upvotes: 1

Related Questions