Skip to content Skip to sidebar Skip to footer

Special Caracters Don't Display Correctly When Splitting

When I'm reading a line in a text file, like this one below : présenté alloué ééé ààà tué And try to print it in the terminal, it displays correctly. But when I apply a

Solution 1:

Printing the list is not the same as printing its elements

s = "présenté alloué ééé ààà tué"print s.split(" ")
forx in s.split(" "):
    printx

Output:

['pr\xc3\xa9sent\xc3\xa9', 'allou\xc3\xa9', '\xc3\xa9\xc3\xa9\xc3\xa9', '\xc3\xa0\xc3\xa0\xc3\xa0', 'tu\xc3\xa9']
présenté
alloué
ééé
ààà
tué

Solution 2:

Python 3.* solution: All you have to do is to specify the encoding you wish to use

f = open("test.txt", encoding='utf-8')
l = f.readline()
f.close()
print(l.split(" "))

And you'll get

['présenté', 'alloué', 'ééé', 'ààà', 'tué']

Python 2.* solution:

import codecs

f = codecs.open("""D:\Source Code\\voc-git\\test.txt""", mode='r', encoding='utf-8')
l = f.read()
f.close()
for word in l.split(" "):
    print(word)

Post a Comment for "Special Caracters Don't Display Correctly When Splitting"