如何使用分隔符|*|拆分合并文本以还原原文件名与内容?
Alright, let's figure out how to reverse that file merging process you did. You've got a single string where each original text file's filename and content were combined, and the whole thing is separated by |*|—now you need to split it back into individual files with their correct names and content.
I'll walk you through a Python solution since it's perfect for this kind of file manipulation, and I'll cover two common scenarios based on how you might have merged the files initially.
Scenario 1: Combined string alternates between filenames and content
If your merged string looks like this:file1.txt|*|This is the content of file1|*|file2.txt|*|And this is file2's content
Here's the code to split and restore:
# Replace this with your actual merged string combined_string = "file1.txt|*|This is the content of file1|*|file2.txt|*|And this is file2's content" # Split the string using your separator split_parts = combined_string.split('|*|') # Iterate over parts in pairs (filename followed by content) for idx in range(0, len(split_parts), 2): # Skip incomplete pairs (in case the string ends with a separator) if idx + 1 >= len(split_parts): continue filename = split_parts[idx].strip() content = split_parts[idx + 1] # Write the content to the original filename with open(filename, 'w', encoding='utf-8') as file: file.write(content) print("Files restored successfully!")
How this works:
split('|*|')breaks the big string into a list where every even-indexed element is a filename, and the next odd-indexed element is its matching content.- We loop with a step of 2 to grab each filename-content pair cleanly.
strip()cleans up any accidental whitespace around filenames (a common quirk if you had trailing spaces during the merge process).- Using
with openensures files are closed properly after writing, and specifyingutf-8prevents encoding issues with special characters in your content.
Scenario 2: Each file's entry is "filename\ncontent" separated by |*|
If your merged string was built by first combining each filename and its content with a newline, then joining all entries with |*| (like file1.txt\nThis is file1|*|file2.txt\nThis is file2), use this code instead:
# Replace this with your actual merged string combined_string = "file1.txt\nThis is the content of file1|*|file2.txt\nAnd this is file2's content" split_parts = combined_string.split('|*|') for entry in split_parts: # Skip empty entries (common if the string ends with the separator) if not entry.strip(): continue # Split into filename and content at the first newline (preserves newlines in content) filename, content = entry.split('\n', maxsplit=1) filename = filename.strip() with open(filename, 'w', encoding='utf-8') as file: file.write(content) print("Files restored successfully!")
How this works:
- Each entry in the split list is a full "filename + content" block separated by a single newline.
split('\n', maxsplit=1)splits the entry only once at the first newline, so any newlines in your actual file content are preserved exactly as they were.- We clean up the filename and safely write the content back to its original file.
Quick Notes:
- Run this script in the directory where you want the restored files to be saved.
- If your filenames or content accidentally contain the
|*|separator (you should have avoided this during merging), you'll need to adjust the logic—but this covers the standard use case.
内容的提问来源于stack exchange,提问作者user2618553

