fix,use prepare_db as function foodSearch path

This commit is contained in:
Sirin Puenggun 2023-05-07 22:41:34 +07:00
parent 5a25b4747f
commit 0468c8cdf4
2 changed files with 30 additions and 17 deletions

View File

@ -1,8 +1,16 @@
import sqlite3 import sqlite3
import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
db_path = (current_dir + r"\data\food_data.db")
class FoodSearch: class FoodSearch:
def __init__(self, db_path): def __init__(self):
self.status = os.path.exists(db_path)
if self.status:
self.conn = sqlite3.connect(db_path) self.conn = sqlite3.connect(db_path)
else:
raise FileNotFoundError
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
def search(self, user_input) -> list: def search(self, user_input) -> list:

View File

@ -1,24 +1,29 @@
import os
import sqlite3 import sqlite3
import pandas as pd import pandas as pd
import re import re
thai_df = pd.read_csv('thai_data.csv') def prepare_db():
us_df = pd.read_csv('us_data.csv')
japan_df = pd.read_csv('japan_data.csv')
conn = sqlite3.connect('food_data.db') dfs = []
for i in range(1, 5):
filename = f'Essential/data/us_data_{i}.csv'
df = pd.read_csv(filename)
dfs.append(df)
thai_df = thai_df[~thai_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] thai_df = pd.read_csv('Essential/data/thai_data.csv')
japan_df = japan_df[~japan_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] us_df = pd.concat(dfs, ignore_index=True)
us_df = us_df[~us_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] japan_df = pd.read_csv('Essential/data/japan_data.csv')
thai_df = thai_df.dropna(subset=['product_name']) current_dir = os.path.dirname(os.path.abspath(__file__))
japan_df = japan_df.dropna(subset=['product_name']) conn = sqlite3.connect(current_dir + r"\data\food_data.db")
us_df = us_df.dropna(subset=['product_name'])
thai_df.to_sql('thai_food', conn, if_exists='replace', index=False) # Clean
us_df.to_sql('us_food', conn, if_exists='replace', index=False) combined_df = pd.concat([thai_df, us_df, japan_df], ignore_index=True)
japan_df.to_sql('japan_food', conn, if_exists='replace', index=False) combined_df = combined_df[~combined_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)]
combined_df = combined_df.dropna(subset=['product_name'])
combined_df.to_sql('food_data', conn, if_exists='replace')
conn.commit() conn.commit()
conn.close() conn.close()