2017-05-29 76 views
0
from textblob import TextBlob as tb 
from sqlalchemy import create_engine 
import pandas as pd 

作爲engine=create_engine("mysql+mysqldb://root:[email protected]:3306/listing")UnicodeDecodeError錯誤:在284位置「ASCII」編解碼器不能解碼字節0x93:順序不在範圍內(128)

然後我用read_sql命令讀取大熊貓第一i-創造了發動機使用SQLAlchemy的來自數據庫的數據。

df=pd.read_sql('select locationId,text from location_reviews',engine)

試圖將文本列從字符串轉換爲textblob UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 284: ordinal not in range(128) 我使用SQLAlchemy的和df = pd.read_sql(query,engine)從SQL讀取數據時,我得到這個錯誤。 然後我試圖使用

df['text']=df.text.apply(lambda x: tb(x)) 

並獲得上述錯誤的文字列轉換在textblob。

+0

您的SQL包含非ASCII數據。你是如何創建數據框的?顯示你的代碼(或者更好,簡單的_working_例子)。哪條線給你的錯誤?顯示堆棧跟蹤。當您創建數據框時,問題可能出在哪裏? – alexis

回答

0

這意味着您在嘗試轉換爲TextBlob的文本中有一個unicode字符。你可能想要確保沒有unicode字符偷偷靠過。

嘗試unidecode,東西沿着這些線路:

from unidecode import unidecode 
df['text']=df.text.apply(lambda x: tb(unidecode(x))) 
+0

不起作用回報相同的錯誤 –

+0

然後錯誤可能發生在代碼中的這一點之前。你能找出錯誤發生的地方嗎?您可能想要將unidecode應用於整個df。 – ystark

+0

解決了這個問題使用https://stackoverflow.com/questions/18649512/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-position-13-ordinal –

相關問題