使用np.genfromtxt

时间:2019-01-22 16:42:49

标签: python numpy genfromtxt

我正在尝试使用np.genfromtxt导入数据文件

数据文件包含一个大的注释标头,每行以注释字符*

开头

在genfromtxt中使用comment ='*'kw时,由于编码怪异,我仍然会报错。谷歌搜索后的编码是CP932,日语字符。

此示例为:b'* HW_ATTACHMENT_NAME“ \ x95W \ x8f \ x80 | Standard” \ r \ n' 可以使用_.decode('cp932')解码为“ * HW_ATTACHMENT_NAME“标准|标准” \ r \ n'

但是,genfromtxt无法将cp932识别为一种编码(传递encoding ='cp932'),并且仍然会引发UnicodeDecodeError。

那么,有没有办法强迫genfromtxt不读这些字符?如果没有,是否可以删除所有cp932编码的字符?

类似这样的东西

with open(file) as f:
    #some code to remove cp932 encoded text
    data = np.genfromtxt(f, comments='*', dtype='float')

edit:这不起作用,可能是不正确的解决方法。

with open(file) as f:  
    lines = (line.decode('cp932') for line in f)  
    data = np.genfromtxt(lines, comments='*', dtype='float')

0 个答案:

没有答案
相关问题