jeudi 17 août 2017

Eror when printing random line from text file

I need to print a random line from the file "Long films".

My code is:

import random
with open('Long films') as f:
    lines = f.readlines()
    print(random.choice(lines))

But it prints this error:

Traceback (most recent call last):
line 3, in <module>
    lines = f.readlines()
line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 36: ordinal not in range(128)

What do I need to do to not get this error?




Aucun commentaire:

Enregistrer un commentaire