如何用Python匹配两个CSV文件的Code字段并生成合并结果文件?
Code Column Got it, let's tackle this problem. You need to combine data from Callnum.csv and F-V-Mnum.csv where their Code values match, then save the result to final.csv. Here are two straightforward ways to do this in Python:
Method 1: Using Python's Built-in csv Module (No External Dependencies)
This approach uses Python's standard library, so you don't need to install anything extra. We'll first build a lookup map from F-V-Mnum.csv to quickly find matching Group and PersonID values for each Code, then iterate through Callnum.csv to write merged rows.
import csv # Create a dictionary to map Code to (Group, PersonID) from F-V-Mnum.csv code_lookup = {} with open('F-V-Mnum.csv', mode='r', newline='', encoding='utf-8') as fvm_file: reader = csv.DictReader(fvm_file) for row in reader: code = row['Code'] code_lookup[code] = (row['Group'], row['PersonID']) # Process Callnum.csv and write merged data to final.csv with open('Callnum.csv', mode='r', newline='', encoding='utf-8') as call_file, \ open('final.csv', mode='w', newline='', encoding='utf-8') as final_file: call_reader = csv.DictReader(call_file) # Define the output columns in your desired order output_columns = ['SubjectID', 'Name', 'Code', 'Group', 'PersonID'] final_writer = csv.DictWriter(final_file, fieldnames=output_columns) final_writer.writeheader() for row in call_reader: current_code = row['Code'] if current_code in code_lookup: group, person_id = code_lookup[current_code] # Build the merged row merged_row = { 'SubjectID': row['SubjectID'], 'Name': row['Name'], 'Code': current_code, 'Group': group, 'PersonID': person_id } final_writer.writerow(merged_row)
How This Works:
- We first load
F-V-Mnum.csvand store eachCodeas a key incode_lookup, with the correspondingGroupandPersonIDas values. This makes lookups fast (O(1) time per row). - Then we read
Callnum.csvline by line. For each row, if itsCodeexists in our lookup map, we create a new merged row with all the required fields and write it tofinal.csv.
Method 2: Using pandas (Simpler for Larger Datasets)
If you're working with bigger files or want more concise code, pandas is a great tool. It handles CSV merging with just a few lines of code. First, install pandas if you haven't already:
pip install pandas
Then use this script:
import pandas as pd # Load both CSV files into DataFrames callnum_df = pd.read_csv('Callnum.csv') fv_mnum_df = pd.read_csv('F-V-Mnum.csv') # Perform an inner join on the 'Code' column (only keeps matching rows) merged_df = pd.merge(callnum_df, fv_mnum_df, on='Code', how='inner') # Reorder columns to match your desired output format merged_df = merged_df[['SubjectID', 'Name', 'Code', 'Group', 'PersonID']] # Save the merged DataFrame to final.csv (without the index column) merged_df.to_csv('final.csv', index=False)
How This Works:
pd.merge()withhow='inner'ensures only rows with matchingCodevalues from both files are kept.- We reorder the columns to match your requested output structure, then save the result to
final.csv.
When you run either script, your final.csv will have the matching row from your example:
SubjectID,Name,Code,Group,PersonID 67,Attila,1134112,1,78
内容的提问来源于stack exchange,提问作者jack




