如何在Python输出中包装和缩进字典中的字符串? [英] How can I wrap and indent strings from dictionary in Python output?

查看:69
本文介绍了如何在Python输出中包装和缩进字典中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包装输出文本时我需要帮助.我需要打印参考,即书本+章节+经文编号和实际经文.但是,我需要该行以100个字符换行,然后与已经打印的第一部分对齐.

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.

我有:

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

在本节中:

        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. '

因此,我希望输出的换行符为100个字符,然后缩进,使其与原始缩进匹配.

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

部分日志文件:

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.

推荐答案

使用 textwrap 模块,以一定的线宽将文本换行,并可选地处理缩进.

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)

这会将您的 record 文本包装为100个字符的宽度,然后使除第一行外的整个文本缩进11个空格;您最终得到的文字宽度为111个字符.

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.

这篇关于如何在Python输出中包装和缩进字典中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆