Pandas DataFrame的重音字符在Excel中出现乱码

Pyd*_*man 5 python csv excel non-ascii-characters pandas

附:

# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)

在我的.ipynb顶部,Jupyter现在正确显示重音字符.

当我导出到csv(with .to_csv())pandas包含重音字符的数据框时:

在此输入图像描述

...在Excel中打开csv时,字符无法正确呈现.

在此输入图像描述

无论我是否设置都是这种情况encoding='utf-8'.pandas/python是否可以在这里完成所有这些,这是一个Excel问题?或者可以在导出到csv之前完成某些事情?

  • Python:2.7.10
  • 熊猫:0.17.1
  • Excel:Excel for Mac 2011

Jul*_*era 8

如果您想保留口音,请尝试使用 encoding='iso-8859-1'

df.to_csv(path,encoding='iso-8859-1',sep=';')
Run Code Online (Sandbox Code Playgroud)


小智 1

如果您无法让 Excel 配合,也许可以在您的列中尝试此功能。它将使用unicodedata库删除重音符号:

import unicodedata

def remove_accents(input_str):

    if type(input_str) == unicode:
        nfkd_form = unicodedata.normalize('NFKD', input_str)
        return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
    else:
        return input_str
Run Code Online (Sandbox Code Playgroud)