错误(双重)编码CSV的反向传播

0 投票
1 回答
91 浏览
提问于 2025-04-14 17:38

我有一个CSV文件,但这个文件的编码方式搞错了。

这个文件是一个电影数据库,里面有对应的演员信息。我下载这个文件是为了练习一些编程,做一个叫做凯文·贝肯数的项目。

文件的内容大概是这样的:

movieId,title,actors
(...)
61,Eye for an Eye (1996),(a ton of other actors)|Dolores VelÌÁzquez|(more actors)
59,The Confessional (1995),(a ton of other actors)|Richard Fr̩chette|Fran̤ois Papineau|Marie Gignac|Normand Daneau|Anne-Marie Cadieux|Suzanne Cl̩ment|Lynda Beaulieu|Pascal Rollin|Billy Merasty|Paul H̩bert|Marthe Turgeon|Adreanne Lepage-Beaulieu|Andr̩e-Anne Th̩roux-Faille|Rodrigue Proteau|Philippe Paquin|Pierre H̩bert|Nathalie D'Anjou|Danielle Fichaud|Jules Philip|Jacques Laroche|Claude-Nicolas Demers|Jean-Philippe C̫t̩|Tristan Wiseman|Marc-Olivier Tremblay|Jacques Brouillet|Jean-Paul L'Allier|Denis Bernard|Ren̩e Hudon|Serge Laflamme|Carl Mathieu
(...)

你可以看到,演员的名字本该有一些特殊的字母,比如带有变音符号的字母(ÄÖÜ,É,À,Û等),但现在却变成了其他一些奇怪的符号。

通过对这个问题的讨论,我们发现这确实是一个叫做乱码的情况。

我的目标是通过编程来修复这些错误的字符,方法是按照正确的顺序进行解码和编码。

1 个回答

0

这是最接近解决方案的内容(由JosefZ提供):

你遇到了一个双重乱码的情况(Python示例):'FrÌ©chette|Fran̤ois'。经过一系列编码和解码操作,最终得到的结果是'Fréchette|François'。

感谢大家对这个问题的热心帮助,我们确认这确实是一个乱码的案例。

我在以下配置下取得了一些进展(Python):

  1. 读取当前的csv文件,编码没有指定
  2. 进行编码和解码操作:encode('cp1252').decode('mac-roman').encode('cp1252').decode('iso-8859-16')
  3. 将结果写入新的csv文件,并指定编码为'iso-8859-16'

通过这个配置,很多字符的问题得到了修复,但仍然有一些字符缺失。我不确定这是否意味着我需要再进行一次解码和编码(总共三次),还是我只是还没有找到正确的两组编码解码的配置。

以下是经过重新编码后仍然出现问题的字符列表:

after re-encoding | just reading | desired outcome | notes
==============================================================================
�                | Ì            | Á               | the lower case á works;
�_               | Ì_           | ü               | the upper case Ü works
�_               | Ì_           | ä               | I can't confirm whether upper case Ä works
                                 | æ               | I can't confirm whether this exists, but I confirmed one upper case that works; it's possible the file uses 'ae' instead
                                 | œ               | I can't confirm whether any of this exists; it's possible the file uses 'oe' instead
�_               | Ì_           | í               | I can't confirm whether upper case Í works
�_               | Ì_           | ó               | the upper case Ó works
�_               | Ì_           | þ               | the upper case Þ doesn't exist, I think

总结:

如你所见,无论是直接读取文件,还是在重新编码后读取,所有缺失的字符都显示为相同的乱码字符,因此似乎无法恢复这些字符的原始信息(这些信息在乱码的过程中丢失了)。我担心这些行需要手动修复。

撰写回答