How do you wrap and indent strings from dictionary in python output? -


i need in wrapping output text. need print reference book+chapter+verse number , actual verse. need line wrap @ 100 characters , align first part 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)         in chapr:             chapter = book + " " + w + ":" + str(i)             record = next((item["verse"] item in store if item["chap"] == chapter), none)             print "%-10r %0r" % (chapter, record)   bible_read() 

in section:

        in chapr:             chapter = book + " " + w + ":" + str(i)             record = next((item["verse"] item in store if item["chap"] == chapter), none)             print "%-10r %0r" % (chapter, record) 

i need able print out like:

'gen 1:2'  ' , earth without form, , void; , darkness upon               face of deep. , spirit of god moved upon face of              waters. ' 

so output wrap @ 100 characters , indent matches original indent.

part of logfile:

2 | gen 1:3 | , god said, let there light: , there light.  3 | gen 1:4 | , god saw light, good: , god divided light darkness.  4 | gen 1:5 | , god called light day, , darkness called night. , evening , morning first day.  5 | gen 1:6 | , god said, let there firmament in midst of waters, , let divide waters waters.  6 | gen 1:7 | , god made firmament, , divided waters under firmament waters above firmament: , so.  7 | gen 1:8 | , god called firmament heaven. , evening , morning second day.  

update: (my final code)


from textwrap import wrap import sys  cmd1 = sys.argv[1] cmd2 = sys.argv[2] cmd3 = sys.argv[3]   logfile = "kjv.txt"  if cmd1 == "lookup":  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()  chapter = cmd2 + " " + cmd3  print "\n"  if ':' not in chapter:     book = chapter.split(" ")[0]     w = chapter.split(":")[0]     w = w.split(" ")[1]      chapter = book + " " + str(w)      item in store:       if chapter in item["chap"]:         wrapped_lines = wrap(item["verse"], 100)         indented_lines = ("\n" + ' ' * 13).join(wrapped_lines)         print "%-9s - %0s \n" % (item["chap"], indented_lines)  elif '-' 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)      chapr = range(x,y+1)      in chapr:         chapter = book + " " + str(w) + ":" + str(i)          record = next((item["verse"] item in store if item["chap"] == chapter), none)         wrapped_lines = wrap(record, 100)         indented_lines = ("\n" + ' ' * 13).join(wrapped_lines)         print "%-9s - %0s \n" % (chapter, indented_lines)  else:      record = next((item["verse"] item in store if item["chap"] == chapter), none)      wrapped_lines = wrap(record, 100)      indented_lines = ("\n" + ' ' * 13).join(wrapped_lines)      print "%-9s - %0s \n" % (chapter, indented_lines) 

use textwrap module wrap text @ linewidth, , optionally handle indentation.

to wrap lines @ width, add own indentation, use:

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

this wraps record text 100 characters wide, indents whole text except first line 11 spaces; end text 111 characters wide.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -