954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print (X) number of words on a line

I am brand-new to Python, and this is my first post on DaniWeb. Seems like a positive community, and I hope one day to contribute as a knowledgable expert and not a world-class newbie.

I'm wanting to create a simple python program that "fakes" Shakespeare or haiku-style output. I realize actual poems are more a matter of syllables, but I'm just getting my toes wet.

I want to create a program that can take an example.txt file of plain text and output its contents in either 10-word lines or in 7-5-7 word lines; the number of words in a line is arbitrary.

smittles
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

You want random words or just re-splitting the lines with word count based line breaks?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Just re-splitting in the same order, based on the word count, yeah.

smittles
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

I would do something like this, don't be shy of starting to use yield to generate answers instead of making one time use lists without purpose.

bookfile = '11.txt'
pattern = (7, 5, 7)

def nwords(book, nwords):
    for dontcare in range(nwords):
        ## give word from begining (0) and remove from list of words one by one
        yield book.pop(0)
    ## finish giving words after nwords has been given 

with open(bookfile) as thebook:
    # read text of book and split from white space
    bookaslist =  thebook.read().split()
    # until bookaslist is empty, which is considered False value
    while bookaslist:
        for count in pattern:
            # rejoin count words and center it for 60 column print
            print ' '.join(nwords(bookaslist, count)).center(60)
        # empty line between to clarify the form
        print
Attachments 11.txt (163.6KB)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Wow, great start and great response. I am getting errors, though (Python 3.1 is what I'm compiling in)
At first I was getting this error:

File "/Users/jonmitten/Documents/workspace/LyndaPython3EssentialTraining/poemizer.py", line 24
    print ' '.join(nwords(bookaslist, count)).center(60)
            ^
SyntaxError: invalid syntax


After adding parathesis to

print ' '.join(nwords(bookaslist, count)).center(60))

to

print( ' '.join(nwords(bookaslist, count)).center(60)))

I get this error:

Traceback (most recent call last):
  File "/Users/jonmitten/Documents/workspace/LyndaPython3EssentialTraining/poemizer.py", line 24, in <module>
    print( ' '.join(nwords(bookaslist, count)).center(60))
  File "/Users/jonmitten/Documents/workspace/LyndaPython3EssentialTraining/poemizer.py", line 14, in nwords
    yield book.pop(0)
IndexError: pop from empty list
smittles
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Ok the yield should be:

from __future__ import print_function

bookfile = '11.txt'
pattern = (7, 5, 7)

def nwords(book, nwords):
    for dontcare in range(nwords):
        ## give word from begining (0) and remove from list of words one by one
        if book:
             yield book.pop(0)
        else:
             break
    ## finish giving words after nwords has been given 

with open(bookfile) as thebook:
    # read text of book and split from white space
    bookaslist =  thebook.read().split()
    # until bookaslist is empty, which is considered False value
    while bookaslist:
        for count in pattern:
            # rejoin count words and center it for 60 column print
            print(' '.join(nwords(bookaslist, count)).center(60))
        # empty line between to clarify the form
        print()

This means that it will return incorrect number of words from the end of the file, when the words finish at end of the file.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Wow, very cool. I'm going to have to look over the yield method to see what you did there.

Thanks for the decent head-start :)

smittles
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Yield is handy, but you can get sometimes confused with it. Main headaches is, that sometimes you think you are putting values to list, but you put generator of values or you forget that once generated the values of generator are lost, and finally that after generator finishes, it is empty and you must recreate it to start again.

Here is one infinite generator of numbers up from starting value. It can not be used in for loop without exit statement, because it has not exit condition. After each yield the state of function is saved and the execution continues forward to next result.

def countup(x):
    while True:
        yield x
        x+=1

countfrom10= countup(10)
print(next(countfrom10))
print(next(countfrom10))
for i in range(10):
    print(next(countfrom10))

print(next(countfrom10))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: