简体   繁体   中英

How to read multiple csv files at the same time in pandas

Hi i am trying to get the user to select three files and then have them be read into the program after the user has selected and pressed to open. I have tried the following code but i get the following error message. I would like it to print out the filename once opened which works when i had the code to open a single file at a time but i would like it to work for multiple files as well just not sure if its possible or not.

> Exception in Tkinter callback Traceback (most recent call last):  
> File "C:\Users\John\anaconda3\lib\tkinter\__init__.py", line 1883, in
> __call__
>     return self.func(*args)   File "<ipython-input-7-0367338b5787>", line 24, in file_opener
>     read_file = pd.read_csv (filename, "r","w",error_bad_lines=False, engine="python")   File
> "C:\Users\John\anaconda3\lib\site-packages\pandas\io\parsers.py", line
> 676, in parser_f
>     return _read(filepath_or_buffer, kwds)   File "C:\Users\John\anaconda3\lib\site-packages\pandas\io\parsers.py", line
> 430, in _read
>     fp_or_buf, _, compression, should_close = get_filepath_or_buffer(   File "C:\Users\John\anaconda3\lib\site-packages\pandas\io\common.py",
> line 200, in get_filepath_or_buffer
>     raise ValueError(msg) ValueError: Invalid file path or buffer object type: <class 'tuple'>

My code is as follows.

import pandas as pd
import tkinter as tk
import csv
import json
import numpy as np
import re
import matplotlib.pyplot as plt
import fileinput
from tkinter import filedialog
from tkinter import messagebox
from tkinter import Menu



root = tk.Tk()
root.title("Data Tools 101")
root.geometry("650x700")


def file_opener():
    global read_file

    filename = filedialog.askopenfilenames(initialdir = "/", title = "Open files", multiple=True)
    read_file = pd.read_csv (filename, "r","w",error_bad_lines=False, engine="python")


    df = pd.read_csv('Inspections.csv')
    print(filename)

menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Open File',command = file_opener)
filemenu.add_separator()
menubar.add_cascade(label='File', menu=filemenu)

root.config(menu=menubar)
root.mainloop()

The documentation for tkinter.filedialog.askopenfilenames is pretty thin, but considering that it lets you select multiple files, its a good bet that the return is a collection of some kind. Sure enough, if you move print(filename) above the failing line, it shows that a tuple of filenames is returned. So loop through those names to create the dataframes.

import json
import numpy as np
import re
import matplotlib.pyplot as plt
import fileinput
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import Menu
import pandas as pd

root = tk.Tk()
root.title("Data Tools 101")
root.geometry("650x700")

def file_opener():
    global read_file

    filenames = filedialog.askopenfilenames(initialdir = "/", title = "Open files", multiple=True)
    dfs = []
    for filename in filenames:
        print("opening", filename)
        dfs.append(pd.read_csv (filename, error_bad_lines=False, engine="python"))
        print(dfs[-1])

menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Open File',command = file_opener)
filemenu.add_separator()
menubar.add_cascade(label='File', menu=filemenu)

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