2016-12-12 14 views
1

日本語の文章とふりがなと呼ばれる文字を含むSQLite3データベースがあり、読み上げに役立ちます。SQLite3のテキスト処理Pythonのデータベース

私は、文字列を処理してフリガナ文字のない文字列を返すことができるremove_furiganaという関数を持っています。しかし、この関数を渡すと、文章はデータベースから取り除かれても効果がないようです。誰かが私のためにここで何が起こっているのかを明確にして、解決策の方向に向けることができますか?

def remove_furigana(content): 
    furigana = False 
    expression = "" 
    for character in content: 
     if character == '[': 
      furigana = True 
     elif character == ']': 
      furigana = False 
     elif not furigana: 
      expression += character 
    return expression.replace(" ", "") 

def retrieve_article(): 
    c.execute('SELECT content FROM sentence WHERE article_id = "k10010770581000"') 
    for row in c.fetchall(): 
     print(remove_furigana(row)) 
+2

としてアクセスできます。 'row'は実際に_tuple_です。あなたの関数に何が送られているかを見るには 'print(row)'を表示してみてください。 –

答えて

0

Python SQLite fetchall functionは、そのレコードのフィールドで構成されるタプルを返します。あなたは、関数にcontent列を送信する必要があります。

また
def retrieve_article(): 
    c.execute('SELECT content FROM sentence WHERE article_id = "k10010770581000"') 
    for row in c.fetchall(): 
     print(remove_furigana(row[0])) 

、あなたは辞書ではなくタプルを取得するためにrow_factoryを使用することができます。

import sqlite3 

def dict_factory(cursor, row): 
    d = {} 
    for idx, col in enumerate(cursor.description): 
     d[col[0]] = row[idx] 
    return d 

con = sqlite3.connect(":memory:") con.row_factory = dict_factory 

その場合には、fetchAllの結果は辞書で、あなたでしょうcontentフィールドには、

print(remove_furigana(row['content'])) 
関連する問題