This code reads CSV files in a specified folder and saves the data into an Excel file. The code uses the pandas library to read the CSV files and manipulate the data, and the openpyxl library to write the data into an Excel file.

Here is a breakdown of the code:

  1. Import the necessary libraries:

    • pandas for data manipulation
    • os for file operations
    • re for regular expressions
    • openpyxl for Excel file operations
  2. Define the folder path where the CSV files are located, the file format to match, and the output folder and file name for the Excel file.

  3. Iterate through the files in the folder:

    • Check if the filename matches the specified file format using regular expressions.
    • Extract the date from the filename using string slicing.
    • Read the CSV file into a pandas DataFrame, selecting only the required columns.
    • Filter the data based on specific conditions using boolean indexing.
    • If the output file is not yet created, create it and save the filtered data as the first sheet.
    • If the output file already exists, open it in append mode and add the filtered data as a new sheet.
    • Print a message indicating that the file has been processed.
  4. Print a message indicating that all files have been processed.

The code assumes that all the required libraries are installed and that the folder and file paths are valid

import pandas as pdimport osimport refrom openpyxl import Workbook#读取指定路径下的CSV文件folder_path = 自动验单数据自动验单发生率四月file_format = r自动验单发生率统计详情d8csvoutput_folder = F自动验单数据45-55验本数据output_file = Nonefor filena

原文地址: https://www.cveoy.top/t/topic/ihxf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录