如何通过编程将两个文本文件中的变更合并至单个文件?
How to Merge FUNCTION Blocks from Two Files (Not Concatenate)
Got it, let's figure out how to merge the FUNCTION blocks from your two files properly—no simple concatenation here, we need to match up the corresponding function sections and combine their content. Let's break this down:
1. First, Clarify the Merge Goal
Looking at your examples:
FILE_Ahas clean FUNCTION blocks with basic statementsFILE_Bhas the same FUNCTION structure, but adds comments to some statements- The end goal is to merge these so we keep the core statements from
FILE_Aplus the comments fromFILE_Bfor matching statements, grouped by each FUNCTION name.
Example Inputs
FILE_A Content:
FUNCTION FUNCTION1 STATEMENT STATEMENT11 STATEMENT STATEMENT12 ; FUNCTION FUNCTION2 STATEMENT STATEMENT21 STATEMENT STATEMENT22 ;
FILE_B Content:
FUNCTION FUNCTION1 STATEMENT STATEMENT11 #this is comment1 STATEMENT STATEMENT12 ; FUNCTION FUNCTION2 STATEMENT STATEMENT21 #this is comment2 STATEMENT STATEMENT22 ;
2. Manual Merge Approach
If you're doing this by hand, follow these steps for each FUNCTION:
- Locate the same FUNCTION name in both files (e.g.,
FUNCTION1in FILE_A and FILE_B) - For each statement in the FUNCTION block:
- If the statement exists in both files but FILE_B has a comment, use the FILE_B version (keep the comment)
- If a statement is unique to one file, add it to the merged block
- Keep the
FUNCTIONheader and closing;intact
Merged FILE_C Result:
FUNCTION FUNCTION1 STATEMENT STATEMENT11 #this is comment1 STATEMENT STATEMENT12 ; FUNCTION FUNCTION2 STATEMENT STATEMENT21 #this is comment2 STATEMENT STATEMENT22 ;
3. Automated Merge with Python Script
If you have many functions or need to repeat this, a simple script can handle the merge automatically. This script will parse both files, group statements by FUNCTION name, and merge them while preserving comments from FILE_B:
def parse_function_file(file_path): """Parse a file into a dictionary of {function_name: list_of_lines}""" functions = {} current_func = None current_lines = [] with open(file_path, 'r') as f: for line in f: stripped_line = line.strip() if not stripped_line: continue # Start of a new function if stripped_line.startswith('FUNCTION'): # Save the previous function if we had one if current_func is not None: functions[current_func] = current_lines current_func = stripped_line.split()[1] current_lines = [stripped_line] # End of a function block elif stripped_line == ';': current_lines.append(stripped_line) functions[current_func] = current_lines current_func = None current_lines = [] # Regular statement line else: current_lines.append(stripped_line) return functions # Parse both input files file_a_functions = parse_function_file('FILE_A') file_b_functions = parse_function_file('FILE_B') # Merge functions: prioritize FILE_B's annotated lines over FILE_A's merged_functions = {} all_function_names = set(file_a_functions.keys()).union(file_b_functions.keys()) for func_name in all_function_names: a_lines = file_a_functions.get(func_name, []) b_lines = file_b_functions.get(func_name, []) # Create a map of core statements (without comments) to their annotated versions from FILE_B statement_map = {} for line in b_lines: if not line.startswith('FUNCTION') and line != ';': core_statement = line.split('#')[0].strip() statement_map[core_statement] = line # Build merged lines: use FILE_B's annotated lines where possible, else FILE_A's merged_lines = [] for line in a_lines: if line.startswith('FUNCTION') or line == ';': merged_lines.append(line) else: core_line = line.strip() merged_lines.append(statement_map.get(core_line, line)) # Add any unique lines from FILE_B that aren't in FILE_A (edge case handling) for line in b_lines: if line not in merged_lines: merged_lines.append(line) merged_functions[func_name] = merged_lines # Write the merged content to FILE_C with open('FILE_C', 'w') as output_file: for func_lines in merged_functions.values(): output_file.write('\n'.join(func_lines) + '\n\n')
How to Use the Script:
- Save it as
merge_functions.py - Place your
FILE_AandFILE_Bin the same folder - Run it with
python merge_functions.py - Your merged content will be in
FILE_C
内容的提问来源于stack exchange,提问作者Shivam
相关产品推荐
相关产品推荐

