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

将交互式Python代码片段整合为CSV清理脚本的最佳实践咨询

Got it, let's break down how to properly combine those three CSV cleaning steps into a single, reliable script. The main issues when merging interactive snippets usually come from sloppy file handling, unordered steps, or lack of error checks. Here's a polished, robust version with explanations of the best practices:


Full Integrated Script

#!/usr/bin/env python
import csv
import os

# Configurable parameters - tweak these based on your data
INPUT_FILE = "commas.csv"
TEMP_DELIMITER = "|"  # Pick a character NOT present in your original CSV
COMMA_REPLACEMENT = ";"  # What to replace field-internal commas with
FINAL_FILE = "cleaned_commas.csv"
INTERMEDIATE_FILE1 = "temp1.csv"
INTERMEDIATE_FILE2 = "temp2.csv"

def swap_csv_delimiter(input_path, output_path, old_delimiter, new_delimiter):
    """Safely replace the delimiter of a CSV file"""
    with open(input_path, "r", newline="", encoding="utf-8") as infile, \
         open(output_path, "w", newline="", encoding="utf-8") as outfile:
        reader = csv.reader(infile, delimiter=old_delimiter)
        writer = csv.writer(outfile, delimiter=new_delimiter)
        writer.writerows(reader)

def clean_field_commas(input_path, output_path, delimiter, replacement):
    """Replace commas inside CSV fields with the specified replacement"""
    with open(input_path, "r", newline="", encoding="utf-8") as infile, \
         open(output_path, "w", newline="", encoding="utf-8") as outfile:
        reader = csv.reader(infile, delimiter=delimiter)
        writer = csv.writer(outfile, delimiter=delimiter)
        for row in reader:
            cleaned_row = [field.replace(",", replacement) for field in row]
            writer.writerow(cleaned_row)

def main():
    try:
        # Step 1: Swap original comma delimiter to a temporary one
        swap_csv_delimiter(INPUT_FILE, INTERMEDIATE_FILE1, ",", TEMP_DELIMITER)
        print(f"Step 1 done: Swapped delimiter to {TEMP_DELIMITER} (saved to {INTERMEDIATE_FILE1})")

        # Step 2: Clean up commas inside fields
        clean_field_commas(INTERMEDIATE_FILE1, INTERMEDIATE_FILE2, TEMP_DELIMITER, COMMA_REPLACEMENT)
        print(f"Step 2 done: Replaced field commas with {COMMA_REPLACEMENT} (saved to {INTERMEDIATE_FILE2})")

        # Step 3: Swap back to comma delimiter
        swap_csv_delimiter(INTERMEDIATE_FILE2, FINAL_FILE, TEMP_DELIMITER, ",")
        print(f"Step 3 done: Restored comma delimiter (final file: {FINAL_FILE})")

        # Optional: Clean up temporary files
        os.remove(INTERMEDIATE_FILE1)
        os.remove(INTERMEDIATE_FILE2)
        print("Cleaned up temporary files")

    except FileNotFoundError:
        print(f"Error: Input file {INPUT_FILE} not found")
    except Exception as e:
        print(f"Unexpected error during processing: {str(e)}")

if __name__ == "__main__":
    main()

Key Best Practices for Integration

  1. Use Context Managers (with Statements)
    This is non-negotiable for file handling in Python. It automatically closes files after use, preventing resource leaks and corruption that can happen with manual open()/close() calls.

  2. Modularize Functions
    Breaking each core task into a separate function makes the code easier to read, test, and modify. If you need to tweak how delimiters are swapped later, you only change one function instead of digging through a monolithic script.

  3. Avoid In-Place File Modification
    Using intermediate temporary files protects your original data if something goes wrong mid-process. You can always skip deleting them temporarily if you need to debug each step.

  4. Add Configurable Parameters
    Putting file names and special characters at the top means you don't have to hunt through code to adjust settings for different CSV files. Just update the variables and go.

  5. Include Basic Error Handling
    Catching common issues like missing input files gives clear feedback instead of a cryptic traceback, making the script usable for non-developers too.

  6. Validate Your Temporary Delimiter
    Make sure TEMP_DELIMITER doesn't exist anywhere in your original CSV. If you're unsure, run a quick scan of the file first, or use a rare control character like chr(29) (ASCII Record Separator) which almost never appears in user data.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:34:53