【问题标题】:wrap all lines that are longer than line length包装所有长于行长的行
【发布时间】:2013-11-12 03:50:58
【问题描述】:

我正在编写一个将每行限制为一定长度的程序。

这是我目前得到的,我差不多完成了,但我仍然需要剪掉每一行,但我无法弄清楚。

def main():
    filename = input("Please enter the name of the file to be used: ")
    openFile = open(filename, 'r+')
    file = openFile.read()
    lLength = int(input("enter a number between 10 & 20: "))

    while (lLength < 10) or (lLength > 20) :
        print("Invalid input, please try again...")
        lLength = int(input("enter a number between 10 & 20: "))

    wr = textwrap.TextWrapper()
    wraped = wr.wrap(file)

    print("Here is your output formated to a max of", lLength, "characters per line: ")

    wr.width = lLength
    wr.expand_tabs = True
    for lines in wraped:
        print(lines)

编辑:

def main():
    filename = input("Please enter the name of the file to be used: ")
    openFile = open(filename, 'r')
    file = openFile.read()
    lLength = int(input("enter a number between 10 & 20: "))

    while (lLength < 10) or (lLength > 20) :
        print("Invalid input, please try again...")
        lLength = int(input("enter a number between 10 & 20: "))
    if (lLength > 10) or (lLength < 20):
        print("\nYour file contains the following text: \n" + file)
#=========================================================================
        wr = textwrap.TextWrapper(width=lLength)
        wraped = wr.wrap(file)

        print("\n\nHere is your output formated to a max of", lLength, "characters per line: ")

        for lines in wraped:
            print(lines)

main()

输出应该是这样的一个例子。 如果指定的文件包含此文本:

hgytuinghdt #here the length is 11
ughtnjuiknshfyth #here the length is 16
nmjhkaiolgytuhngjuin #here the length is 20

并且 lLength 被指定为 15,那么这应该打印出来:

hgytuinghdt
ughtnjuiknshfyt
h
nmjhkaiolgytuhng
juin

【问题讨论】:

标签: python python-3.x computer-science


【解决方案1】:

wr = textwrap.TextWrapper() 应该是wr = textwrap.TextWrapper(width=length, expand_tabs=True) 然后您应该删除wr.width = lLengthwr.expand_tabs = True。它们应该在wr.wrap() 被调用之前运行,但由于可以使用TextWrapper 构造函数中的关键字参数来设置它,如最顶部所示,它们可以被删除。

PS:如果你使用TextWrapper.fillfor lines in wraped: print(lines) 可以替换为print(wraped)

【讨论】:

  • 当我这样做时,行限制不对,当我限制为 15 时,例如它限制为其他内容。
  • @user1919840 这是因为 wrap 函数返回一个元素列表。请改用fill(我已经更新了答案)。
  • @user1919840 换行不会断字。您可以改为寻找 split()。
  • @AnkitJaiswal:当然会,除非你明确设置 [break_long_words=False](extwrap.html#textwrap.TextWrapper.break_long_words)。
  • @user1919840 那是因为fill()返回一个字符串,所以for循环遍历该字符串的每个字符。
【解决方案2】:

试试这个:

line = 'nmjhkaiolgytuhngjuin'
n = 5 # set the width here
a = [line[i:i+n] for i in range(0, len(line), n)]
print ("\n".join(a))

【讨论】:

  • 这 (a) 不是有效的 Python 3.x,并且 (b) 与 OP 试图做的事情不同。 textwrap 将尝试在空格上换行,并且仅在单词长于 width 时才分词;您的代码只会破坏每个 n 字符,而不会尝试将单词保持在一起。
  • @abarnert 我同意 OP 要求文本换行,但是他在上述答案中的 cmets 表明他正在寻找这样的东西。
  • 这几乎是我想要的,但仍然不完全是,它在第​​一次“包裹”之后没有正确包裹.....嗯,这不是这里需要的包裹
【解决方案3】:

这可能会对您有所帮助:

def wrap(f,n):
        fi=open(f,"w+")
        rl=fi.readlines()    
        for i in range(0,len(rl)):
            if len(rl[i]) > n:
                fi.write(rl[i][:n] +"\n" + rl[i][n:])
                fi.Close()
            else:
                fi.write(rl[i])
                fi.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2022-12-18
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多