简体   繁体   English

创建一个 Class,它通过读取多个 CSV 文件来创建字典 - Python

[英]Create a Class that creates a dictionary from reading in Multiple CSV Files - Python

I have 24 csv files that currently reside in a folder directory.我有 24 个 csv 文件当前驻留在文件夹目录中。 The goal is to read all the CSV files in and store them as individual pandas dataframes.目标是读取所有 CSV 文件并将它们存储为单独的 pandas 数据帧。 At the request of my client, they wish all of our code to be in Object Oriented Programming.应我的客户的要求,他们希望我们所有的代码都在 Object 面向编程中。 I am new to OOP and I would appreciate any help.我是 OOP 的新手,我将不胜感激。

I am currently trying to create a class that will read in my files and store them as a dictionary via a for loop.我目前正在尝试创建一个 class ,它将读取我的文件并通过 for 循环将它们存储为字典。 With the key being the name of the file, and the value being the pandas dataframe键是文件名,值是 pandas dataframe

I already have a list of filepaths stored in aa variable called fns我已经有一个文件路径列表存储在一个名为 fns 的变量中

This is what I have for the code so far, I jam trying to figure out the loop logic so I don't have to call a new class instance every time.到目前为止,这就是我所拥有的代码,我试图找出循环逻辑,所以我不必每次都调用一个新的 class 实例。

fns = glob.glob(path + "*.csv")
enc = 'ISO-8859-1'

# create class

class MyFile:

    def __init__(self, file_path):
        self.file = file_path

    def ParseName(self):
        self.name_me = self.file.split('\\')[-1].strip('.csv')

    def Read_CSV(self):
        self.data_csv = pd.read_csv(self.file,delimiter='\t',
                                    low_memory=False, encoding= enc)

My goal is to get a dictionary like this:我的目标是得到这样的字典:

{'filename1': DataFrame, 'filename2': DataFrame, .... 'filename24': DataFrame} {'filename1': DataFrame, 'filename2': DataFrame, .... 'filename24': DataFrame}

I appreciate all the help!我感谢所有的帮助!

Sample Object-oriented CsvStorage :面向对象的示例CsvStorage

import glob
import pandas as pd
from os.path import basename

class CsvStorage:

    _dfs = {}

    def __init__(self, path):
        for f in glob.glob(path):
            self._dfs[basename(f)] = pd.read_csv(f, encoding='ISO-8859-1')

    def get_dataframes(self):
        if not self._dfs:
            raise ValueError('No dataframes. Load data first')

        return self._dfs

files_path = '*/FILE_*.csv'   # adjust to your actual path pattern
csv_store = CsvStorage(files_path)
dfs = csv_store.get_dataframes()

print(dfs)

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

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