In this tutorial, we will delve into the world of dynamic text manipulation in Python. Specifically, we’ll guide you through the process of creating a Python script that iterates over characters in each line of a file, inserts random characters, and repeats the process across multiple lines.
1. Iterating Over Characters in a Word.
- Source code.
import random def add_random_characters(word, char_list): modified_word = "" for char in word: modified_word += char + random.choice(char_list) return modified_word def call_add_random_characters(): # Example usage input_word = 'example' char_list = ['-', ' ', '@#$', '**'] modified_word = add_random_characters(input_word, char_list) print(f"Original Word: {input_word}") print(f"Modified Word: {modified_word}") if __name__ == "__main__": call_add_random_characters()
- Output.
Original Word: example Modified Word: e@#$x@#$a m**p l**e-
2. Dynamically Inserting Random Characters Across Lines.
- Source code.
import random def add_random_characters(word, char_list): modified_word = "" for char in word: modified_word += char + random.choice(char_list) return modified_word def process_line(line, char_list): modified_line = "" for word in line.split(): modified_word = add_random_characters(word, char_list) modified_line += modified_word + ' ' return modified_line.strip() def call_process_line(): # Example usage input_line = 'This is a sample line.' char_list = ['$', '#', '@', '&'] modified_line = process_line(input_line, char_list) print(f"Original Line: {input_line}") print(f"Modified Line: {modified_line}") if __name__ == "__main__": call_process_line()
- Output.
Original Line: This is a sample line. Modified Line: T$h&i&s@ i@s& a# s&a&m&p$l&e@ l@i#n#e$.#
3. Applying Across Each Line in a File.
- Source code.
import random def add_random_characters(word, char_list): modified_word = "" for char in word: modified_word += char + random.choice(char_list) return modified_word def process_line(line, char_list): modified_line = "" for word in line.split(): modified_word = add_random_characters(word, char_list) modified_line += modified_word + ' ' return modified_line.strip() def across_each_line_in_a_file(): # Example usage input_file_path = './resource-files/input_file.txt' output_file_path = './resource-files/output_file.txt' char_list = ['-', ' ', '@', '*', '#', '$'] with open(input_file_path, 'r') as input_file, open(output_file_path, 'w') as output_file: for line in input_file: modified_line = process_line(line, char_list) output_file.write(modified_line + '\n') print('task complete.') if __name__ == "__main__": across_each_line_in_a_file()
- Output on the console.
task complete.
- The input content in file ./resource-files/input_file.txt.
hello python today is a good day
- The output content in file ./resource-files/output_file.txt.
h#e*l$l*o$ p y-t@h o@n* t$o@d a#y* i*s* a g#o*o*d$ d#a$y#
4. Conclusion.
- In this tutorial, we’ve demonstrated how to dynamically iterate over characters in a word, insert random characters, and apply this process across each line in a file using Python.
- This flexible script can be adapted for various text manipulation tasks, providing a dynamic and customizable approach to text transformation in your projects.
- Remember to customize file paths, character lists, and other parameters based on your specific needs.