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

如何修复含非法分隔符的CSV文件?逐行处理多余分号并标记异常行的实现方案

Hey there! Let's fix that tricky CSV issue you're facing. The problem is super common when dealing with free-text fields that contain your delimiter—let's break down how to solve it properly.

Step 1: Choose the Right Processing Approach

You don't need to load the entire file into memory first. Reading, processing, and writing line-by-line is the most efficient way (especially for large files), as it saves memory and lets you handle the data in real time. Our core goals are:

  • Keep the first two semicolons (;) as field separators
  • Replace all semicolons in the third (free-text) field with commas (,)
  • Flag the line numbers of originally corrupted rows

Step 2: Fixed and Improved Code

Your initial code had the right idea, but was missing the critical step of writing cleaned data to a new file, and trying to read the raw corrupted CSV with pandas would throw errors. Here's the complete, working version:

# Define input/output file paths
input_file = "sample.csv"
output_file = "cleaned_sample.csv"

# Process the file line by line
with open(input_file, encoding="UTF-8") as infile, open(output_file, "w", encoding="UTF-8") as outfile:
    # Write the header row first (it should be formatted correctly)
    header = next(infile)
    outfile.write(header)
    
    # Iterate over data rows, starting line count at 2 (since header is line 1)
    for line_num, line in enumerate(infile, start=2):
        # Remove trailing newline to avoid formatting issues
        line = line.rstrip("\n")
        # Split the line by semicolons
        parts = line.split(";")
        
        # Check if the row is corrupted (normal rows split into 3 parts)
        if len(parts) > 3:
            print(f"损坏的行号: {line_num} 内容: {line}")
            # Join all parts after the first two with commas
            cleaned_text = ", ".join(parts[2:])
            # Reconstruct the valid CSV line
            new_line = f"{parts[0]};{parts[1]};{cleaned_text}\n"
        else:
            # Normal rows get written as-is (add back the newline)
            new_line = line + "\n"
        
        # Write the cleaned/normal line to the output file
        outfile.write(new_line)

# Now safely read the cleaned CSV with pandas
import pandas as pd
df = pd.read_csv(output_file, encoding="utf-8", sep=";")
print("\n处理后的DataFrame预览:")
print(df)

Step 3: What This Code Does

  • Handles the header separately to ensure it stays intact
  • Uses enumerate(start=2) to count lines like we do in real life (header = line 1, first data row = line 2)
  • Detects corrupted rows by checking if splitting with ; gives more than 3 parts
  • Prints corrupted line numbers and their original content for your reference
  • Reconstructs valid lines by replacing semicolons in the free-text field with commas
  • Lets you safely load the cleaned file into pandas without format errors

Step 4: Verify the Result

After running the code, your cleaned_sample.csv will match your expected output exactly:

customerId;name;text
1;Josey;I want to go at 05pm
2;Mike;Check this out --> öl
2;Frank;This is a longer text and,ups that should not be
2;Max;okay,
3;Josey;here is everythink good

And your console will show the corrupted rows you need to note:

损坏的行号: 4 内容: 2;Frank;This is a longer text and;ups that should not be
损坏的行号: 5 内容: 2;Max;okay;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 23:07:30