在控制台上连续将Pandas数据追加为一行,并在终端机/控制台上显示

时间:2019-04-03 21:39:04

标签: python python-3.x pandas

我有10个CSV文件,每个CSV文件都具有相同的列数,以熊猫数据框的形式从中逐一读取数据。我希望这些数据以某种表格形式显示在控制台/终端上。而且应该就像每次数据进入新行一样。有什么建议吗?

以下是我的示例CSV文件: enter image description here

像这样,有10个或更多CSV文件,我将一一读取这些文件中的数据,并希望显示在控制台/终端上。

我的应用程序简介

我有一台在一定时间间隔后将CSV文件生成到文件夹中的机器。我正在使用Watchdog库将手表放在正在生成CSV文件的文件夹中。当我收到CSV文件时,将其读入熊猫数据框。上面提供了示例CSV文件。

只要机器正在运行,它就会继续生成CSV文件。因此,如果我想查看需要打开每个CSV文件的数据,则需要一个视图,其中在生成新的CSV文件时更新数据。

因此从技术上讲,正在读取的一个CSV文件被转换为数据框,然后在控制台/终端上打印。并且,当生成新的CSV文件时,该过程再次发生。但是,当新的数据帧到达时,它不应覆盖整个控制台,而是附加到控制台上的现有数据。

这是我的主文件:

import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import pandas as pd
from Append_Function import append_df_to_excel
import os.path
import sys

class Watcher:
    def __init__(self, args):
        self.watch_dir = os.getcwd()
        print(args[0])
        self.directory_to_watch = os.path.join(self.watch_dir, args[1])
        self.observer = Observer()
        self.event_handler = Handler(patterns=["*.CSV"], ignore_patterns=["*.tmp"], ignore_directories=True)

    def run(self):
        self.observer.schedule(self.event_handler, self.directory_to_watch, recursive=False)
        self.observer.start()
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()
            print("Error")

        self.observer.join()


class Handler(PatternMatchingEventHandler):
    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None
        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print("Received created event - %s." % event.src_path)
            df = pd.read_csv(event.src_path, header=1, index_col=0)
            append_df_to_excel(os.path.join(os.getcwd(), "myfile.xlsx"), df)
        elif event.event_type == 'modified':
            # Taken any actionc here when a file is modified.
            df = pd.read_csv(event.src_path, header=0, index_col=0)
            append_df_to_excel(os.path.join(os.getcwd(), "myfile.xlsx"), df)
            print("Received modified event - %s." % event.src_path)


if __name__ == '__main__':
    print(sys.argv)
    w = Watcher(sys.argv)
    w.run()

这是我的附加功能:

import pandas as pd
import openpyxl as ox


def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
                       truncate_sheet=False,
                       **to_excel_kwargs):
    # ignore [engine] parameter if it was passed

    if 'engine' in to_excel_kwargs:
        to_excel_kwargs.pop('engine')

    writer = pd.ExcelWriter(filename, engine='openpyxl')

    # Python 2.x: define [FileNotFoundError] exception if it doesn't exist
    try:
        FileNotFoundError
    except NameError:
        FileNotFoundError = IOError

    try:
        # try to open an existing workbook
        writer.book = ox.load_workbook(filename,keep_vba=True)

        # get the last row in the existing Excel sheet
        # if it was not specified explicitly
        if startrow is None and sheet_name in writer.book.sheetnames:
            startrow = writer.book[sheet_name].max_row

        # truncate sheet
        if truncate_sheet and sheet_name in writer.book.sheetnames:
            # index of [sheet_name] sheet
            idx = writer.book.sheetnames.index(sheet_name)
            # remove [sheet_name]
            writer.book.remove(writer.book.worksheets[idx])
            # create an empty sheet [sheet_name] using old index
            writer.book.create_sheet(sheet_name, idx)

        # copy existing sheets
        writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
    except FileNotFoundError:
        # file does not exist yet, we will create it
        pass

    if startrow is None:
        startrow = 0

    # write out the new sheet
    df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs, header=True)

    # save the workbook
    writer.save()

0 个答案:

没有答案