我是python的新手。这就是我卡住的地方。我需要将CSV导入python并创建一个搜索引擎,该引擎需要通过输入信息来准确定位小包或大包的数据。我正在尝试放入状态信息。如果我想输入2015年之类的年份,如何消除不在2015年调查部分中的州?例如,我将“ 2015”放置在“状态”框中,它将不会出现任何“ 2016年调查”状态。最终,该搜索引擎如何为用户提供他们通过消息窗口搜索的已售出的小号大包的确切数据? 非常感谢您的帮助。 Here is the csv file link,
import tkinter
import tkinter.ttk
import tkinter.messagebox
import csv
#Create the GUI
root = tkinter.Tk()
root.title("Avocado Search Engine")
root.configure( bg = "khaki1")
def show():
"""Shows the data in cvs file"""
global state_box
global year_box
with open("avocado.csv") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_file:
if state_box.get() == "Albany" and size_box.get() == "Small bag":
print(row[8])
#State label
state_label= tkinter.Label(root, text = "Choose the state")
state_label.grid(row = 0, column = 0)
state_label.configure( bg = "khaki1")
#State box
state_box = tkinter.ttk.Combobox(root, values = ["Albany","Atlanta","Boise"], state = "readonly")
state_box.grid(row = 0, column = 1)
state_box.current(0)
#Size label
size_label= tkinter.Label(root, text = "Choose the bag size")
size_label.grid(row = 0, column = 2)
size_label.configure( bg = "khaki1")
#Size box
size_box = tkinter.ttk.Combobox(root, values = ["Small bag","Large bag"], state = "readonly")
size_box.grid(row = 0, column = 3)
size_box.current(0)
#Show box
show_button = tkinter.Button(root, text = " Search", command = show)
show_button.configure( bg = "spring green")
show_button.grid(row = 0, column = 4)
#Quit
quit_button = tkinter.Button(root, text = " Quit", command = root.destroy)
quit_button.configure( bg = "red2")
quit_button.grid(row = 1, column = 4)
root.mainloop()
答案 0 :(得分:0)
使用熊猫库。 要将csv导入python文件,请使用: df = pd.read_csv(“ csv的完整路径”)
例如df = pd.read_csv(“ C:/user/user/Desktop/mycsv.csv”)
要基于标准获取信息,请考虑使用loc。
df.loc [df。(您要用于比较的列)== 2015]