简体   繁体   English

如何在pandas中同时读取多个csv文件

[英]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.嗨,我试图让用户访问 select 三个文件,然后在用户选择并按下打开后将它们读入程序。 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. tkinter.filedialog.askopenfilenames的文档非常薄,但考虑到它可以让您 select 多个文件,它的返回值是某种集合是一个不错的选择。 Sure enough, if you move print(filename) above the failing line, it shows that a tuple of filenames is returned.果然,如果您将print(filename)移到失败的行上方,则表明返回了一个文件名tuple 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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