1

I need help in wrapping my output text. I need to print the reference which is the book+chapter+verse number and the actual verse. However I need the line to wrap at 100 characters and then align with the first part already printed.

I have:

from operator import itemgetter

logfile = "kjv.txt"

def bible_read():
    fp=open(logfile,'r')
    store=[]
    while 1:

        line=fp.readline()

        if not line:
                    break
        if line[-1:] == '\n':
                    line=line[:-1]

        data0=line.split(' ')[0]
        data1=line.split(' | ')[1]
        data2=line.split('|')[2]
        store.append({'LINE':data0,'Chap':data1,'Verse':data2})
    fp.close()
    #print store[1]

    chapter = raw_input("Enter:")
    if '-' in chapter:
        book = chapter.split(" ")[0]
        w = chapter.split(":")[0]
        w = w.split(" ")[1]
        x = chapter.split(":")[1]
        x = x.split("-")[0]
        x = int(x)
        y = chapter.split("-")[1]
        y = int(y)
        #z = range[x,y]
        #print book, w, x, y
        chapR = range(x,y+1)
        for i in chapR:
            chapter = book + " " + w + ":" + str(i)
            record = next((item["Verse"] for item in store if item["Chap"] == chapter), None)
            print "%-10r %0r" % (chapter, record)


bible_read()

In this section:

        for i in chapR:
            chapter = book + " " + w + ":" + str(i)
            record = next((item["Verse"] for item in store if item["Chap"] == chapter), None)
            print "%-10r %0r" % (chapter, record)

I need to be able to print out like:

'gen 1:2'  ' And the earth was without form, and void; and darkness was upon 
             the face of the deep. And the Spirit of God moved upon the face of
             the waters. '

So I would like to get the output wrap at 100 characters and then indent so it matches up with the original indent.

Part of logfile:

2 | gen 1:3 | And God said, Let there be light: and there was light. 
3 | gen 1:4 | And God saw the light, that it was good: and God divided the light from the darkness. 
4 | gen 1:5 | And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 
5 | gen 1:6 | And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. 
6 | gen 1:7 | And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so. 
7 | gen 1:8 | And God called the firmament Heaven. And the evening and the morning were the second day.

1 Answer 1

3

Use the textwrap module to wrap text at a certain linewidth, and optionally handle indentation.

To wrap lines at a certain width, then add your own indentation, you could use:

from textwrap import wrap

wrapped_lines = wrap(record, 100)
indented_lines = ('\n' + ' ' * 11).join(wrapped_lines)

This wraps your record text to 100 characters wide, then indents the whole text except the first line with 11 spaces; you end up with text 111 characters wide.

Sign up to request clarification or add additional context in comments.

1 Comment

Python3 seems to provide an indent() method: textwrap.indent(text, prefix, predicate=None)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.