编解码器无法解码字节(我看到的解决这个错误的方法也没有帮助)

2024-03-29 09:06:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着从一个数据文件中提取单元,用于它的后处理。这个文件是一个.csv文件,在与pandas打交道之后,我使用pandas作为频道名,跳过(units和“Raw”)后面的2行和数据本身。你知道吗

我单独使用np.genfromtxt文件提取单位:

def get_df(f):
    df = pd.read_csv(os.path.join(pathname, f), skiprows=[0, 1, 2, 3, 4, 6, 7])
    units = np.genfromtxt(os.path.join(pathname, f), skip_header = 6, delimiter = ',', max_rows = 1, dtype = np.string_)

    return df, units

而且,由于其中一些单元包含“/”,我正在更改它们(这些值最终与通道的名称合并,并在生成的绘图的文件名中使用)。你知道吗

df, units = get_df(f)

unit_dict = {}
for column, unit in zip(df.columns, units):
    unit = string.replace(unit, '/', ' per ')
    unit_dict[column] = unit

当我找到一个有学位符号的频道名时,我得到一个错误:

CellAmbTemp �C
Traceback (most recent call last):
  File "filepath_omitted/Processing.py", line 112, in <module> df_average[column], column)
  File "path/Processing.py", line 30, in contour_plot
plt.title(column_name)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 1465, in title
return gca().set_title(s, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 186, in set_title title.set_text(label)
  File "C:\Python27\lib\site-packages\matplotlib\text.py", line 1212, in set_text
self._text = '%s' % (s,)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 12: 
ordinal not in range(128)

Process finished with exit code 1

我打印了一本字典,其中我将频道与单位配对,在本例中,条目如下所示:

'CellAmbTemp': '\xb0C'
  • 那是什么编码?你知道吗
  • 我试过各种各样的方法字符串.解码()和unicode(字符串)以及dtype=unicode\u
  • 有更好的办法吗?或者至少拼凑一些东西来修复它?你知道吗

添加:文件块

Logger description:                                     
Log period: 1 s                                 
Statistics period: 30 s                                 
Statistics window: 300 s                                    
Maximum duration:                                   
Time    Time    Time    ActSpeed    ActTorque   ActPower    FuelMassFlowRate    BarometricPress CellAmbTemp ChargeCoolerInPressG
Date    Time    ms  rev/min Nm  kW  g/h kPa °C  kPa
Raw Raw Raw Raw Raw Raw Raw Raw Raw Raw
1/12/2018   12:30:01 PM 153.4   600.0856308 132.4150085 7.813595703 2116.299996 97.76997785 11.29989827 0.294584802
1/12/2018   12:30:02 PM 153.4   600.1700702 132.7327271 7.989128906 2271.800016 97.76997785 11.29989827 0.336668345
1/12/2018   12:30:03 PM 153.4   600.0262537 128.7541351 7.427545898 2783.199996 97.78462672 11.29989827 0.241980373

预计到达时间:

最后我把我如何获得这些单位的方法改成了熊猫:

def get_df(f):
    df = pd.read_csv(os.path.join(pathname, f), skiprows=[0, 1, 2, 3, 4, 6, 7])
    units = pd.read_csv(os.path.join(pathname, f), skiprows = 6, delimiter = ',')
    units = units.columns
    return df, units

然后我在外面解码/编码:

df, units = get_df(f)

unit_dict = {}
for column, unit in zip(df.columns, units):
    encoding = chardet.detect(unit)['encoding']
    unit = unit.decode(str(encoding)).encode('utf-8')
    unit_dict[column] = unit

现在,当我试图在matplotlib中使用该文本作为绘图标题时,我得到了错误,但是在错误之前,我正在深入研究代码。你知道吗


Tags: 文件csvpathinpydfgetraw
2条回答

您必须知道输入文件的编码(或者只需尝试常见的utf-8)。如果没有,并且utf-8不起作用,请尝试对文件使用chardet,并使用其结果。你知道吗

如果您已经有一个字符串,您可以执行以下操作:

codecs.decode(s, encoding='utf-8')

但是,由于您正在将CSV读取到数据帧,请告诉pd.read_csv您的源代码:

pd.read_csv(..., encoding='utf-8')

当遇到单个字符的问题时,我也使用了一种技巧,我没有费心去解决这个问题,那就是找到并替换。比如:

pd.read_csv(StringIO(open(path).read().replace('\xb0', '')))

不过,这是一个懒惰的选择。你知道吗

相关问题 更多 >