0
我正在使用python3.4從MySQL數據庫表中拉。我使用csv模塊將數據庫中的數據行寫入.CSV格式。現在我正在嘗試使用圓環來弄清楚如何通過可能會在第四列數據中顯示的關鍵字對數據行進行審查(第[3]行)。我正在考慮如下使用re module
,但我不斷收到錯誤。是否無法在字符串類型的字段中搜索關鍵字,並且如果他們具有這些關鍵字,則過濾這些結果?我不斷收到錯誤。請幫助關鍵字搜索字符串從MySQL使用Python?
import re
import csv
userdate = input('What date do you want to look at?')
query = ("SELECT *FROM sometable WHERE timestamp LIKE %s", userdate)
keywords = 'apples', 'bananas', 'cocoa'
# Execute sql Query
cursor.execute(query)
result = cursor.fetchall()
#Reads a CSV file and return it as a list of rows
def read_csv_file(filename):
"""Reads a CSV file and return it as a list of rows."""
for row in csv.reader(open(filename)):
data.append(row)
return data
f = open(path_in + data_file)
read_it = read_csv_file(path_in + data_file)
with open('file.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in data:
match = re.search('keywords, read_it)
if match:
spamwriter.writerow(row)
你得到什麼錯誤?請編輯問題以包含該信息。 – bernie
這一行應該是:'query =(「SELECT * FROM sometable WHERE timestamp LIKE%s」,(userdate,))'第二個參數是一個元組。 – bernie