Return one word at a time

Viewed 75

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())

2 Answers

Just store all the words into a list and then call whatever function you want (in this case I'm calling do_something() on them afterwards:

def do_something(word):
  print(word.upper())

def print_one_word():
    with open("test_script.txt", "r") as f:
        lines = f.readlines()
        all_words = []
        for line in lines:
            for word in line.split():
                all_words.append(word)
                print(word)

    for word in all_words:
      do_something(word)

print_one_word()

output:

stack,
hello
test
python's
name
STACK,
HELLO
TEST
PYTHON'S
NAME

Here is an example of how to return a list and yield from functions, keep in mind yield returns a generator object and if you want to reuse yielded results you need to listify them:

def yield_words():
    with open("test_script.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            for word in line.split():
                yield word

def list_words():
    with open("test_script.txt", "r") as f:
        return [word
                for line in f.readlines()
                for word in line.split()]

def operation(prefix, word):
    print(f'{prefix} {word}')

yielded_words = list(yield_words())
listed_words = list_words()

for word in listed_words:
    operation('listed', word)

for word in yielded_words:
    operation('yielded', word)

Output:

listed this
listed is
listed a
listed test
listed hello
listed there
yielded this
yielded is
yielded a
yielded test
yielded hello
yielded there
Related