Python3新手求助:如何编写脚本移除文本文件中的字符?
Hey there! Let's break this down step by step since you're new to Python—no worries, this is totally manageable. First off, let's tweak your original script to use Python's with statement—it's a safer, cleaner way to handle files because it automatically closes them for you, so you don't have to remember to call .close() at the end.
Then, we'll add the character removal functionality based on what you need. Here are common scenarios and how to implement them:
1. Remove a specific single character (e.g., all commas)
If you want to strip out every instance of one particular character (like ,), use the string replace() method—it replaces all occurrences of the target character with an empty string:
path = '/Users/uname/Desktop/soc-calc/TVs+Assigned+to+Local+Markets.txt' new_path = '/Users/uname/Desktop/soc-calc/tvs.txt' with open(path, 'r') as tvfile, open(new_path, 'w') as new_days: file_content = tvfile.read() # Replace all commas with nothing (removes them) modified_content = file_content.replace(',', '') new_days.write(modified_content) print(modified_content)
2. Remove multiple specific characters (e.g., commas and semicolons)
For removing several different characters at once, use str.maketrans() to create a translation table that maps those characters to None (meaning they get deleted):
path = '/Users/uname/Desktop/soc-calc/TVs+Assigned+to+Local+Markets.txt' new_path = '/Users/uname/Desktop/soc-calc/tvs.txt' # List of characters you want to remove chars_to_remove = ',;:' # Create a translation table to delete these characters translation_table = str.maketrans('', '', chars_to_remove) with open(path, 'r') as tvfile, open(new_path, 'w') as new_days: file_content = tvfile.read() modified_content = file_content.translate(translation_table) new_days.write(modified_content) print(modified_content)
3. Remove a specific substring (e.g., "Local Market")
If you need to delete entire words or phrases instead of single characters, replace() works here too—just pass the substring you want to remove:
path = '/Users/uname/Desktop/soc-calc/TVs+Assigned+to+Local+Markets.txt' new_path = '/Users/uname/Desktop/soc-calc/tvs.txt' with open(path, 'r') as tvfile, open(new_path, 'w') as new_days: file_content = tvfile.read() # Remove every occurrence of the phrase "Local Market" modified_content = file_content.replace('Local Market', '') new_days.write(modified_content) print(modified_content)
4. Remove an entire category of characters (e.g., all numbers)
For more complex removals—like stripping out all digits, spaces, or punctuation—use regular expressions with the re module. First, import re, then use re.sub() to match and replace the pattern:
import re path = '/Users/uname/Desktop/soc-calc/TVs+Assigned+to+Local+Markets.txt' new_path = '/Users/uname/Desktop/soc-calc/tvs.txt' with open(path, 'r') as tvfile, open(new_path, 'w') as new_days: file_content = tvfile.read() # Remove all digits (the pattern \d matches any number) modified_content = re.sub(r'\d', '', file_content) new_days.write(modified_content) print(modified_content)
You can adjust the regex pattern to match other categories:
r'\s'removes all whitespace (spaces, tabs, newlines)r'[^\w\s]'removes all punctuation (leaves letters, numbers, and spaces)
Just pick the method that fits what you need to remove, and adjust the code accordingly!
内容的提问来源于stack exchange,提问作者user3732479

