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

时间:2018-01-18 21:25:40

标签: python python-2.7 pandas numpy

我试图从数据文件中提取单位以用于后期处理。该文件是一个.csv,在与熊猫挣扎之后,我已经使用pandas作为频道名称并跳过了2行(单位和" Raw")和数据本身。 / p>

我单独使用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'
  • 那是什么编码?
  • 我尝试了各种各样的东西,比如string.decode()和unicode(string)和dtype = unicode_
  • 有没有更好的方法来做我需要做的事情?或者至少凑齐一些来解决它?

添加:文件块

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

ETA:

我最终将我获得单位的方式转换为pandas:

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中的绘图标题时,我得到了错误,但我在错误发生之前进入了更远的代码。

2 个答案:

答案 0 :(得分:1)

您必须知道输入文件的编码(或者只是尝试使用公共utf-8)。如果您没有,utf-8不起作用,请尝试在文件上使用chardet并使用其结果。

答案 1 :(得分:0)

如果你已经有了一个字符串,你会这样做:

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', '')))

这是一个懒惰的选择。