首页 > 解决方案 > AttributeError:“str”对象在 pandas read_csv 中没有属性“decode”

问题描述

代码

df=pd.read_csv('x.csv', encoding= 'unicode_escape')

utf-8 给出错误

追溯

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_9236/1406487559.py in <module>
      2                 #for review in train['Short_Description']:
      3 
----> 4 sentences = list(train['Cleaned_Description'].map(lambda x: re.sub("[^a-zA-Z]"," ", (x).decode('utf-8')).lower().split()))
      5 test_sentences = []  # Initialize an empty list of sentences
      6                          #for review in test['Short_Description']:

/opt/conda/lib/python3.7/site-packages/pandas/core/series.py in map(self, arg, na_action)
   4159         dtype: object
   4160         """
-> 4161         new_values = super()._map_values(arg, na_action=na_action)
   4162         return self._constructor(new_values, index=self.index).__finalize__(
   4163             self, method="map"

/opt/conda/lib/python3.7/site-packages/pandas/core/base.py in _map_values(self, mapper, na_action)
    868 
    869         # mapper is a function
--> 870         new_values = map_f(values, mapper)
    871 
    872         return new_values

/opt/conda/lib/python3.7/site-packages/pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

/tmp/ipykernel_9236/1406487559.py in <lambda>(x)
      2                 #for review in train['Short_Description']:
      3 
----> 4 sentences = list(train['Cleaned_Description'].map(lambda x: re.sub("[^a-zA-Z]"," ", (x).decode('utf-8')).lower().split()))
      5 test_sentences = []  # Initialize an empty list of sentences
      6                          #for review in test['Short_Description']:

AttributeError: 'str' object has no attribute 'decode'

标签: pythonpandasunicodestanford-nlpword2vec

解决方案


推荐阅读