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

两CSV文件多列对比并生成Input/Output Trend列的技术实现需求

Solution to Compare Two CSV Files and Generate Trend Analysis

Got it, let's tackle this CSV comparison task. I'll use Python since it's perfect for handling tabular data like this. Here's a step-by-step solution:

Step 1: Script Overview

We'll read both CSV files into dictionaries (using Server name as the key) for fast lookups. Then we'll iterate through each row in the second CSV, calculate percentage changes where servers exist in both files, mark new servers, and output the results as a markdown table.

Step 2: The Python Code

import csv

# Load the first CSV file into a dictionary (key: Server name)
with open('1.csv', 'r') as f:
    reader = csv.DictReader(f)
    old_data = {row['Server']: row for row in reader}

# Process the second CSV file and generate results
results = []
with open('2.csv', 'r') as f:
    reader = csv.DictReader(f)
    for current_row in reader:
        server = current_row['Server']
        path = current_row['Path']
        new_ip = int(current_row['I/P Backlog'])
        new_op = int(current_row['O/P Backlog'])
        
        # Check if the server exists in the first CSV
        if server in old_data:
            old_row = old_data[server]
            old_ip = int(old_row['I/P Backlog'])
            old_op = int(old_row['O/P Backlog'])
            
            # Calculate Input Trend
            if old_ip == new_ip:
                input_trend = "No change"
            else:
                if old_ip != 0:
                    percentage = round(((new_ip - old_ip) / old_ip) * 100)
                    input_trend = f"{percentage}%(up)" if percentage > 0 else f"{abs(percentage)}%(down)"
                else:
                    # Handle case where old value is 0 (new value is non-zero)
                    input_trend = "100%(up)" if new_ip > 0 else "No change"
            
            # Calculate Output Trend
            if old_op == new_op:
                output_trend = "No change"
            else:
                if old_op != 0:
                    percentage = round(((new_op - old_op) / old_op) * 100)
                    output_trend = f"{percentage}%(up)" if percentage > 0 else f"{abs(percentage)}%(down)"
                else:
                    output_trend = "100%(up)" if new_op > 0 else "No change"
            
            results.append([server, path, new_ip, input_trend, new_op, output_trend])
        else:
            # Server is new in the second CSV
            results.append([server, path, new_ip, "New", new_op, "New"])

# Print the results as a Markdown table
print("| Server | Path | I/P Backlog | Input Trend | O/P Backlog | Output Trend |")
print("|--------|------|-------------|-------------|-------------|--------------|")
for row in results:
    print(f"| {row[0]} | {row[1]} | {row[2]} | {row[3]} | {row[4]} | {row[5]} |")

Step 3: Sample Output

When you run this script with your provided CSV files, here's the output (note: corrected the percentage for Server1's Output Trend to match actual data):

ServerPathI/P BacklogInput TrendO/P BacklogOutput Trend
Server1Yes330010%(up)18010%(down)
Server3Yes713%(down)0No change
Server5Yes0No change0No change
Server6Yes10New200New

Notes:

  • The script rounds percentage changes to whole numbers for readability.
  • Handles edge cases where the original value is 0 (marks as 100% up if the new value is non-zero).
  • Ignores servers that exist in the first CSV but not the second, as per your requirements.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:21:46