首页 > 解决方案 > 具有相同字符串的两个不同打印结果

问题描述

我有一个 csv 文件,其中包含使用 utf-8 编码的西班牙语单词和用逗号分隔的英语单词。出于某种原因,如果我打印西班牙语单词,它们仍然包含 utf-8 编码。但是,如果我直接将字符串粘贴到打印语句中,则会显示正确的字符。为什么是这样?

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é.

标签: python

解决方案


问题是open函数会转义\字符。
将此更改open('./Spanish Sentences/Englishsentences.csv')open('./Spanish Sentences/Englishsentences.csv', encoding='unicode_escape')


推荐阅读