如何在不使用Pandas的情况下用Python合并两个CSV文件?
Solution: Merge Two CSVs Without Pandas
Hey there! Let's work through this step by step. Your current code has a couple of key issues (wrong delimiter, exhausted reader from nested loops) but we can fix this by using a lookup dictionary to efficiently match rows by ID.
First, Let's Break Down the Problem
We need to:
- Match rows from the second CSV (
xyz_id) to rows in the first CSV (id) - Combine each matching pair into the output format you specified
- Avoid inefficient nested loops that break the CSV reader
Step-by-Step Fix & Full Code
Here's a complete working solution that addresses your requirements:
import csv import sys # File paths (update these if your paths are different) first_csv = '/home/user/Downloads/FW__Json_FIles/withoutpanda.csv' second_csv = '/home/user/Downloads/FW__Json_FIles/forms.csv' # Step 1: Build a lookup dictionary from the first CSV (using id as key) id_lookup = {} with open(first_csv, 'r') as f: reader = csv.reader(f, delimiter=',') # Fix: Use comma delimiter, not | first_header = next(reader) # Capture the header row for row in reader: row_id = row[3] # id is the 4th column (index 3) id_lookup[row_id] = row # Store the full row for quick lookup # Step 2: Process the second CSV and merge with matching rows from first CSV with open(second_csv, 'r') as f: reader = csv.reader(f, delimiter=',') # Fix: Use comma delimiter here too second_header = next(reader) # Capture the second CSV's header # Combine headers to match your desired output combined_header = second_header + first_header print(','.join(combined_header)) for row in reader: xyz_id = row[0] # Look up the matching row from the first CSV if xyz_id in id_lookup: matching_row = id_lookup[xyz_id] # Combine the two rows and print combined_row = row + matching_row print(','.join(combined_row)) else: # Optional: Handle cases where no matching ID exists print(f"Warning: No matching ID found for xyz_id {xyz_id}", file=sys.stderr)
Key Fixes & Explanations
- Delimiter Correction: Your sample CSVs use commas, but your original code used
|as the delimiter. This was causing the reader to parse each line as a single column instead of splitting values correctly. - Lookup Dictionary: By storing rows from the first CSV in a dictionary with
idas the key, we avoid nested loops (which would exhaust the second CSV reader after the first iteration) and enable O(1) fast lookups. - Header Handling: We explicitly capture headers from both files and combine them to match your desired output structure.
- Error Resilience: Added a warning for cases where an
xyz_idin the second CSV has no matchingidin the first (you can remove this if you don't need it).
What Was Wrong With Your Original Code?
- Nested Loops: The
reader2gets fully consumed on the first iteration ofrow1, so there's no data left for subsequent rows in the first CSV. - Incorrect Delimiter: Using
|instead of,meant the CSV reader wasn't splitting your data into columns properly.
内容的提问来源于stack exchange,提问作者Rakshit Sakhuja
相关产品推荐
相关产品推荐

