简体   繁体   English

在CSV熊猫python中添加特殊字符

[英]Add special characters in csv pandas python

While writing strings containing certain special characters, such as 在编写包含某些特殊字符的字符串时,例如

    Töölönlahdenkatu

using to_csv from pandas , the result in the csv looks like 使用to_csv熊猫 ,结果在CSV样子

    T%C3%B6%C3%B6l%C3%B6nlahdenkatu

How do we get to write the text of string as it is? 我们如何才能按原样写入字符串文本? This is my to_csv command 这是我的to_csv命令

    df.to_csv(csv_path,index=False,encoding='utf8')

I have even tried 我什至尝试过

    df.to_csv(csv_path,index=False,encoding='utf-8')
    df.to_csv(csv_path,index=False,encoding='utf-8-sig')

and still no success.There are other characters replaced with random symbols 仍然没有成功。还有其他字符替换为随机符号

    '-' to –

Is there a workaround? 有解决方法吗?

What you're trying to do is remove German umlauts and Spanish tildes. 您想做的是删除德国变音符和西班牙波浪号。 There is an easy solution for that. 有一个简单的解决方案。

import unicodedata

data = u'Töölönlahdenkatu Adiós Pequeño'
english = unicodedata.normalize('NFKD', data).encode('ASCII', 'ignore')
print(english)

output : b'Toolonlahdenkatu Adios Pequeno' 输出:b'Toolonlahdenkatu Adios Pequeno'

Let me know if it works or if there are any edge cases. 让我知道它是否有效或是否存在任何边缘情况。

Special characters like ö cannot be stored in a csv the same way english letters can. ö这样的特殊字符不能像英文字母一样存储在csv中。 The "random symbols" tell a program like excel to interpret the letters as special characters when you open the file, but special characters cannot be seen when you view the csv in vscode (for instance). “随机符号”告诉诸如excel的程序在打开文件时将字母解释为特殊字符,但是当您在vscode中查看csv时,看不到特殊字符(例如)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM