python - 具有相同字符串的两个不同打印结果
问题描述
我有一个 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é.
解决方案
问题是open
函数会转义\
字符。
将此更改open('./Spanish Sentences/Englishsentences.csv')
为open('./Spanish Sentences/Englishsentences.csv', encoding='unicode_escape')
推荐阅读
- ios - 无法从项目子文件夹中读取文件
- matplotlib - 什么是子图零?(缺乏文件)
- php - Composer 无法在没有 root 的情况下执行命令
- perl - Perl中如何将一个值压入数组?
- python - 按条件查找数据框中行和列的所有索引
- python - 使用 Celery 注册多个任务
- javascript - 使用 RegEx 和 Quill JS 将一组禁用词加粗?
- oracle - 使用 Oracle MERGE INTO 将两个表中的数据合并到第三个表中
- c# - 使用 Span 的 Base64 编码
在 dotnetcore 2.1 - angular - 在触发承诺后从 catch 子句访问“this”