Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different print results with same string

Tags:

python

I have a csv file containing Spanish words with utf-8 encoding and English words separated by commas. For some reason, if I print the Spanish words, they still contain the utf-8 encoding. But, if I paste the string into a print statement directly, the correct characters will display. Why is this?

words = open('./Spanish Sentences/Englishsentences.csv').read().splitlines()
for word in words:
    print(word)
    var = word.split(',')[0]
    print(var)
    print('La abrac\u00e9')
    var = 'La abrac\u00e9.'
    print(var)
La abrac\u00e9.,I hugged her.,He hugged her.,I hugged them.,I gave her a hug.,
La abrac\u00e9.
La abracé
La abracé.
like image 461
themrdan Avatar asked Aug 12 '26 02:08

themrdan


1 Answers

The problem is the open function will escape the \ character.
Change this open('./Spanish Sentences/Englishsentences.csv') to open('./Spanish Sentences/Englishsentences.csv', encoding='unicode_escape')

like image 111
Mahmoud Youssef Avatar answered Aug 13 '26 16:08

Mahmoud Youssef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!