You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python CSV嵌套节点数据提取程序报错排查与批量提取优化需求

Solution to Your CSV-to-Excel Node Extraction Issues

Let's fix both your error and the manual inefficiency problem. Here's a revised version of your code that automatically extracts all nodes from the duct temperature column and writes each to its own Excel column, plus fixes the NoneType unpack error.

Key Issues Addressed:

  1. TypeError Fix: The original readFile function returned None when exceptions occurred (like invalid node numbers), causing the unpack failure. We'll make data parsing more robust and ensure the function always returns valid data.
  2. Automatic Node Extraction: Instead of manual node input, we'll parse the entire list of temperatures from each duct temp cell and create a column for every node automatically.

Revised Code

import os
import csv
import xlwt
import ast

def getInputs():
    directoryFound = False
    csvFileNames = []
    convertedDir = ""
    while not directoryFound:
        folderDir = input("What is the file directory? ")
        convertedDir = folderDir.replace("\\", "/")
        try:
            openFolder = os.listdir(convertedDir)
            csvFileNames = [file for file in openFolder if file.lower().endswith("csv")]
            if csvFileNames:
                directoryFound = True
            else:
                print("No CSV files found in this directory.\n")
        except FileNotFoundError:
            print("That directory doesn't exist.\n")
    
    output = ""
    while not output.strip():
        output = input("What would you like the new file to be called? ")
    
    return output, csvFileNames, convertedDir

def readFile(directory, fileName):
    try:
        with open(f"{directory}/{fileName}", "r") as file:
            reader = csv.reader(file)
            data_rows = []
            time_data = []
            
            # Skip even lines (keeping the original logic of processing odd lines)
            for line_num, line in enumerate(reader):
                if line_num % 2 == 1:
                    data_rows.append(line)
            
            # Extract time column and parse all duct temp nodes
            all_node_data = []
            for row in data_rows:
                time_data.append(row[0])
                # Parse the duct temp string into a Python list using ast.literal_eval (safer than split)
                duct_temp_str = row[2]  # Column 3 (0-indexed is 2) matches your original duct temp column
                try:
                    duct_label, temp_list = ast.literal_eval(duct_temp_str)
                except (SyntaxError, ValueError) as e:
                    print(f"Warning: Failed to parse row {row} - {e}")
                    temp_list = []
                
                # Add each node's temp to its respective column list
                for node_idx, temp in enumerate(temp_list):
                    if node_idx >= len(all_node_data):
                        # Initialize new column list if this is a new node
                        all_node_data.append([])
                    all_node_data[node_idx].append(temp)
            
            # Generate meaningful column names for each node
            column_names = [f"temp_node_{i+1}" for i in range(len(all_node_data))]
            return time_data, all_node_data, column_names
    
    except Exception as e:
        print(f"Error reading file {fileName}: {e}")
        return [], [], []

def main():
    book = xlwt.Workbook()
    output, csv_files, directory = getInputs()
    
    for fileName in csv_files:
        if "junction" in fileName.lower():
            continue  # Skip junction files as per original logic
        
        print(f"Processing {fileName}...")
        time_data, all_node_data, column_names = readFile(directory, fileName)
        
        if not time_data or not all_node_data:
            print(f"Skipping {fileName} - no valid data found")
            continue
        
        # Excel limits sheet names to 31 characters, so truncate if needed
        sheet = book.add_sheet(fileName[:31])
        num_columns = len(all_node_data)
        
        # Write node columns
        for col_idx, (col_name, node_values) in enumerate(zip(column_names, all_node_data)):
            sheet.write(0, col_idx, col_name)
            for row_idx, value in enumerate(node_values, start=1):
                sheet.write(row_idx, col_idx, value)
        
        # Write time column as the last column
        sheet.write(0, num_columns, "time")
        for row_idx, time_val in enumerate(time_data, start=1):
            sheet.write(row_idx, num_columns, time_val)
    
    save_path = f"{directory}/{output}.xls"
    book.save(save_path)
    print(f"File saved successfully to {save_path}")

if __name__ == "__main__":
    main()

What Changed:

  1. Robust Data Parsing: Used ast.literal_eval to safely convert the duct temp string into a Python list, avoiding error-prone split operations that caused issues with large node counts.
  2. Automatic Node Detection: The code now scans every row's duct temp list, creates a column for each node, and populates it with values across all rows—no manual input needed.
  3. Improved Error Handling: Added targeted exception handling to avoid returning None, and skipped files with invalid data instead of crashing the program.
  4. Simplified User Flow: You only need to enter the directory and output filename now—no more repetitive prompts for node numbers or column names.
  5. Excel Compatibility: Truncated sheet names to 31 characters (Excel's limit) to prevent naming errors with long filenames.

How to Use:

  1. Run the script.
  2. Enter the directory containing your CSV files when prompted.
  3. Enter the desired name for the output Excel file.
  4. The script will automatically process all CSV files (excluding those with "junction" in the name), extract every node's temperature data, and write each node to its own column in the Excel sheet (with the time column at the end).

This should resolve both the TypeError and the manual extraction inefficiency. Let me know if you need any adjustments!

内容的提问来源于stack exchange,提问作者yamifm0f

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 00:59:10