简体   繁体   中英

How to open multiple CSV files from a folder one by one, the one admin wants

I have multiple.csv files in a directory and I want to open them one by one on user/admin demand which one he wants to open. And when he opened the file it displays on Tkinter interface in tabular format.

def open_att():
    ..........
    filename = filedialog.askopenfilename(initialdir="C:/Users/Ahmad Jan Ahmadi/Desktop/FYP-11 Final/Attendance/",title="Select Attendance File",filetypes=(("Csv Files","*.csv"),("All Files","*.*")))
    attend= pd.read_csv(filename)
    for row in attend:
        Id = row['Id']
        name = row['Name']
        date = row['Date']
        time = row['Time']
        tree.insert("", 100000, values=(Id, name, date,time))

When I run this program it gives me the error that:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Ahmad Jan Ahmadi\AppData\Local\Programs\Python\Python36-32\lib\tkinter_ init _.py", line 1699, in call return self.func(*args) File "C:\Users\Ahmad Jan Ahmadi\Desktop\FYP-11 Final\main.py", line 438, in open_att Id = row['Id'] TypeError: string indices must be integers

First, I want to ask from the user which.csv file you want to open when he selects one of them then, how to display this file.

I did this fast approach on my phone, needs few optimizations

import os
import glob


os.chdir("/mydir")
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

for name in all_filenames:
   pattern = input("enter your desired file name: ")
   if pattern == name:
       file = name.readlines()
       print(file) 
   else:
       print("file does not exist")
       
       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM