I am trying to open, print, and read a text file that contains special characters such as §. Below is the code I am running:
import codecs
f = codecs.open('sample_text.txt', mode='r', encoding='utf_8')
print f.readline()
The first two lines work, but the third does not. The error code says: Traceback (most recent call last):
"C:\Users\mallikk\Documents\Python Scripts\special_char_test.py", line 6, in <module>
print f.readline()
File "C:\Anaconda2\lib\codecs.py", line 690, in readline
return self.reader.readline(size)
File "C:\Anaconda2\lib\codecs.py", line 545, in readline
data = self.read(readsize, firstline=True)
File "C:\Anaconda2\lib\codecs.py", line 492, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa7 in position 13: invalid start byte
Any ideas? Please let me know if I can clarify anything or add more details. Thank you so much!