I'm new to programming and am playing around with Python scripting.
I'm trying to write a Python script that will read a text file and print to screen, search for a word, and every time it finds the word, to split the data of that line.
The test.txt file looks something like:
ape bear cat dog ape elephant frog giraffe ape horse iguana jaguar
ape bear cat dog
ape elephant frog giraffe
ape horse iguana jaguar
file = "test.txt"
read_file = open(file, "r")
with read_file as data:
word = "ape"
for word in data:
fileName = "test.txt" read_file = open(fileName, "r") with read_file as open_file: data = open_file.read().rstrip() keyword = "ape" data = ' '.join(["\n"*(word == keyword) + word for word in data.split()]).strip() # data = data.replace(keyword, "\n"+keyword).strip() print(data)
# ape bear cat dog # ape elephant frog giraffe # ape horse iguana jaguar