첫 번째 단계는 mysql에서 데이터를 가져 오는 것으로 시작됩니다. 그런 다음 내가 만든 사전으로 맞춤법 검사기를 수행합니다. 마지막으로 mysql에 출력을 삽입합니다. 문제는 여러 번 반복되는 출력이 있다는 것입니다.
내 코드 :
app.py
from dic import dikk
cur.execute("SELECT a FROM table WHERE id=%s", [id])
data = cur.fetchall()
for row in data:
for k, v in dikk.items():
t = re.compile(re.escape(k), re.IGNORECASE)
row['a'] = t.sub(v, row['a'])
print(row['a'])
mySql_insert_query = """INSERT INTO table2 (a) VALUES (%s)"""
records_to_insert =[(row['a'])]
cur = mysql.connection.cursor()
cur.execute(mySql_insert_query, records_to_insert)
# Commit to DB
mysql.connection.commit()
dic.py
dikk={'speling':'spelling','writen':'written','Jhon':'John'}
내가 얻은 출력 :
it is not Jhon
it is not Jhon
it is not John
내 원하는 출력 :
it is not John
3 개의 키 값 쌍이있는 사전을 반복하고 각 루프에 대해 및 호출 print
하여 insert
3 번 트리거되도록합니다.
당신이 통화를 이동하는 경우 insert
와 print
루프의 외부에, 당신은 요소마다 1 개 개의 반환해야합니다.
dikk={'speling':'spelling','writen':'written','Jhon':'John'}
data = [('it is not Jhon',), ('you spell it speling',), ('writen is the way',)]
for idx, value in enumerate(data):
for word in value[0].split(" "):
if word in dikk.keys():
data[idx] = data[idx][0].replace(word, dikk[word])
# Insert can go here
for row in data:
print(row)
# Or insert can go here
#it is not John
#you spell it spelling
#written is the way
의 insert
각 행에를 추가 data
하거나 나중에 data
목록을 다시 반복하여 개별적으로 삽입 할 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다