打开包含未定义字符的文件(csv.gz)

问题描述 投票:0回答:1

我有一个函数,传递的参数是 5 个文件路径。但是,第一个路径是 csv.gz,其中文件内部似乎存在未定义的字符。我该如何解决这个问题?

我使用的是Python 3.11.1版本。代码和错误消息如下所示。

function(r"filepath1", r"filepath2", r"filepath3", r"filepath4", r"filepath5")

错误信息:

Cell In[3], line 8, in function(filepath1, filepath2, filepath3, filepath4, filepath5)
 6 file1DateMap = {}
 7 infd = open(file1path1, 'r')
 8 infd.readline()
 9 for line in infd:
10     tokens = line.strip().split(',')
 
File ~\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py:23, in IncrementalDecoder.decode(self, input, final)
22 def decode(self, input, final=False):
23     return codecs.charmap_decode(input,self.errors,decoding_table)[0]
 
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 94: character maps to <undefined>

我试过了

file = open(filename, encoding="utf8")

但是在我的 Python 版本中编码未定义。

我尝试了“打开”方法

file2 = r"file2path"
file3 = r"file3path"
file4 = r"file4path"
file5 = r"file5path"
file1name = r"file1path"
with open(file1name, 'r') as file1:
    function(file1, file2, file3, file4, file5)

但该函数需要一个字符串:

TypeError:需要 str、bytes 或 os.PathLike 对象,而不是 TextIOWrapper

我期待该函数运行并将处理后的输出写入桌面上的文件夹。

我仔细检查了文件的编码,它是UTF 8。我编写了以下代码:

with open(r"path1", encoding="utf8") as openfile1:
    file1 = openfile1.read()

收到此错误:

UnicodeDecodeError:“utf-8”编解码器无法解码位置 1 中的字节 0x8b:起始字节无效

python csv error-handling unicode decode
1个回答
0
投票

此源代码中存在一些令人困惑的地方。

with open(file1name, 'r') as file1:
    function(file1, file2, file3, file4, file5)

请理解

file1
是一个打开的文件句柄, 带有 TextIOWrapper 的
type(...)
。 它是可迭代的,您可以从中请求文本行。 相比之下,
file2
等人。是
str
路径名; 您无法从这些对象请求文件系统文本行。

您为它们选择的并行命名结构 可能会让自己感到困惑加上任何不幸的维护 在未来几个月内遇到此代码的工程师。 建议您采用类似

path2
..
path5
.

的名字

您的默认编码似乎是 代码页1252。 您请求使用

open(file1name, 'r')
进行编码 省略可选的
encoding=
参数。 请注意,
mode='r'
是默认值, 所以你也可以把那个排除在外。

相比之下,

open(filename, encoding="utf8")
使用完全不同的编码打开读取访问。

编码是底层 .CSV 文件的属性, 而不是你的程序。 也就是说,您必须知道正确的底层编码是什么, 并且您必须告诉

open
正确的编码。 您可以默认执行此操作,也可以明确执行此操作, 只要你做对了。 我建议明确地这样做。

如果您不知道编码, 使用

/usr/bin/file
/usr/local/bin/iconv
、 或文本编辑器来了解它是什么, 也许将其更改为 UTF-8 如果您对其当前的编码不满意。

大多数现代机器上的大多数文件 应该是 UTF-8 编码——否则的话 就是自找麻烦。但我离题了。

一旦您选择了一些已知的编码, 通过

open
将其传递给
encoding=
参数,您就可以开始营业了!

© www.soinside.com 2019 - 2024. All rights reserved.