簡體   English   中英

預期的字符串或類似字節的對象NLTK Mysql

[英]expected string or byte-like object NLTK Mysql

您能幫我解決這個錯誤嗎?

def get_db():
    mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="cosmos"
    )
    mycursor = mydb.cursor()
    sql_select="SELECT article FROM crawling_sm"
    mycursor.execute(sql_select)
    data=mycursor.fetchall()
    for z in range(len(data)):
        text_sents=sent_tokenize(data[z])

def process_text(text_article):
    text_sents=text_article
    text_sents_clean = [remove_string_special_characters(s) for s in text_sents] #if s.istitle() == False]
    doc_info = get_doc(text_sents_clean)
    #freqDict_list = create_freq_dict(text_sents_clean)
    #TF_scores = computeTF(doc_info, freqDict_list)
    print(text_sents)

    get_db()

錯誤消息是我從數據庫中選擇的文章無法拆分為某些文本,我嘗試使用send_tokenize,但是存在一些錯誤消息,期望該字符串或類似字節的對象

錯誤消息:

_slices_from_text中的文件“ C:\\ Users \\ HP Laptop \\ Anaconda3 \\ lib \\ site-packages \\ nltk \\ tokenize \\ punkt.py”,行1295,用於匹配self._lang_vars.period_context_re()。finditer(text):

TypeError:預期的字符串或類似字節的對象

問題是

data = mycursor.fetchall()

返回元組列表,即使查詢返回的是單列。

因此,與其返回類似

['a', 'b', 'c', 'd', 'e', 'f']

它返回

[('a',), ('b',), ('c',), ('d',), ('e',), ('f',)]

解決方案是將每個元組的第一個元素傳遞給sent_tokenize函數。

for row in data:
    text_sents = sent_tokenize(row[0])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM