I have a function that takes in a text file and prints each word in the file one by one, each on a new line.
Here's the code that does what I said above:
def print_one_word():
with open("test_script.txt", "r") as f:
lines = f.readlines()
word_current = []
for line in lines:
for word in line.split():
word_current.append(word)
print("".join(word_current))
word_current.clear()
Example, with specified text file content:
test_script.txt has the content (as formatted): stack, hello test python's name
The function print_one_word() will print the following to stdout (as formatted):
stack,
hello
test
python's
name
The goal is to pass each word, one by one, into a second function that will perform some operations on it (Ex: capitalize the first letter). However, the most important part is that it sends to the second function only one word, and once the operation is performed, the next word is sent.
To do this, I've replaced print with return, after realizing that that wouldn't work (would only send one word), I tried using yield. However, it still only sends one word to the second function and then stops (it won't continue sending the following words).
I've also tried methods besides what I have currently (creating a list of one word, printing that list without formatting, and then clearing the list), such as simply printing word and so forth. Unfortunately, I got the same result; I could print each word one at a time, but not send to the second function each word one at a time.
Does anyone have any suggestions? Thanks in advance.
Edit, for clarity:
The second function takes in an argument, and I use the first function as the argument.
Example:
def operation(script):
does some things
operation(print_one_word())