如何用Python3.6.4提取多行9个空格后内容并输出至两个文本文件
Solution for Batch Processing Text Files with Python 3.6.4
Got it, let's work through this problem together. I've put together a Python 3.6.4-compatible script that handles batch processing of your text files, extracts content after 9 spaces per line, skips lines containing personal:01, and splits the final results into two output files.
The Script
import os def process_files(input_files, output_file1, output_file2): processed_lines = [] for file_path in input_files: # Skip files that don't exist if not os.path.isfile(file_path): print(f"Oops, {file_path} doesn't exist — skipping this one.") continue with open(file_path, 'r', encoding='utf-8') as input_file: for line in input_file: # Skip lines with 'personal:01' if 'personal:01' in line.strip(): continue # Extract content after 9 characters (adjust if you mean 9 consecutive spaces) if len(line) >= 9: # Take everything after the 9th character extracted_content = line[9:].rstrip('\n') processed_lines.append(extracted_content) else: # Optional: Handle lines shorter than 9 chars — here we keep the whole line processed_lines.append(line.rstrip('\n')) # Split processed lines into two equal-ish parts split_point = len(processed_lines) // 2 first_half = processed_lines[:split_point] second_half = processed_lines[split_point:] # Write to first output file with open(output_file1, 'w', encoding='utf-8') as out1: out1.write('\n'.join(first_half) + '\n') # Write to second output file with open(output_file2, 'w', encoding='utf-8') as out2: out2.write('\n'.join(second_half) + '\n') # -------------------------- # Replace these with your files! # -------------------------- if __name__ == "__main__": # List all your input text files here YOUR_INPUT_FILES = [ "data1.txt", "data2.txt", "more_files_here.txt" ] # Name your output files OUTPUT_FILE_1 = "results_part1.txt" OUTPUT_FILE_2 = "results_part2.txt" # Run the processing process_files(YOUR_INPUT_FILES, OUTPUT_FILE_1, OUTPUT_FILE_2) print(f"Done! Check {OUTPUT_FILE_1} and {OUTPUT_FILE_2} for your results.")
Key Notes & Adjustments
- 9 Spaces vs 9 Characters: The script above extracts content after the 9th character (regardless of what those first 9 are). If you specifically need to extract content only after 9 consecutive leading spaces, replace the extraction part with this:
# Check if line starts with 9 spaces if line.startswith(' '): # That's 9 spaces! extracted_content = line[9:].rstrip('\n') processed_lines.append(extracted_content) else: # Optional: Skip lines without 9 leading spaces, or keep them — adjust as needed continue - Encoding: If your text files use a different encoding (like
gbkfor Chinese), update theencodingparameter inopen()calls to match. - Short Lines: The script keeps lines shorter than 9 characters. If you want to skip these instead, just remove the
elseblock and the line that appends the short line.
How to Use
- Replace
YOUR_INPUT_FILESwith the full or relative paths to all your text files. - Set
OUTPUT_FILE_1andOUTPUT_FILE_2to your desired output file names. - Run the script with Python 3.6.4 — it’ll process all files, skip the unwanted lines, split the results, and save them to your two output files.
内容的提问来源于stack exchange,提问作者Raj Banra
相关产品推荐
相关产品推荐

