包装文本文件,以便每行包含最多80个字符

Ash*_*ary -1 python string python-3.x

是否有更好的方法来解决这个问题,最好不要通过模块.

问题是:

文字处理.您厌倦了在电子邮件包装上看到行,因为人们为您的邮件阅读器应用程序键入的行太长.创建一个程序来扫描长度超过80个字符的所有行的文本文件.对于每个违规行,找到80个字符前最接近的单词并在那里打破该行,将剩余的文本插入下一行(并将下一行向下推一行).完成后,不应有超过80个字符的行.

让9-16.txt的内容是:

Text Processing. You are tired of seeing lines on your e-mail wrap because people type lines that are too long for your mail reader application. Create a program to scan a text file for all lines longer than 80 characters. For each of the offending lines, find the closest word before 80 characters and break the line there, inserting the remaining text to the next line (and pushing the previous next line down one). When you are done, there should be no lines longer than 80 characters.
Run Code Online (Sandbox Code Playgroud)

我的计划实现这一目标

f=open('9-16.txt','r')

lis=[]
def ding(a):

    if len(a)<=80:
        lis.append(a)
        return
    else:
        if a[79]==' ':

            lis.append(a[:80])
            ding(a[80:])

        elif a[79]!=' ':
            ind=a.rfind(' ',0,79)

            lis.append(a[:ind+1])
            ding(a[ind+1:])

for x in f:
    if len(x)>80:
        ding(x)
    else:
        lis.append(x)

ty=open('9-16o.txt','w')
for x in lis:
    if x[-1]==' ':
        x=x[:-1]+'\n'
    else :
        x+='\n'    
    ty.write(x)
f.close()
ty.close()
Run Code Online (Sandbox Code Playgroud)

9-16o.txt现在包含:

Text Processing. You are tired of seeing lines on your e-mail wrap because
people type lines that are too long for your mail reader application. Create a
program to scan a text file for all lines longer than 80 characters. For each
of the offending lines, find the closest word before 80 characters and break
the line there, inserting the remaining text to the next line (and pushing the
previous next line down one). When you are done, there should be no lines
longer than 80 characters.
Run Code Online (Sandbox Code Playgroud)

And*_*ark 8

这是一个使用正则表达式的相当简单和简洁的解决方案,它可能不适用于您的作业,但它应该明确为什么限制模块使用是不可取的:

import re

s = 'Text Processing. You are tired of seeing lines on your e-mail wrap because people type lines that are too long for your mail reader application. Create a program to scan a text file for all lines longer than 80 characters. For each of the offending lines, find the closest word before 80 characters and break the line there, inserting the remaining text to the next line (and pushing the previous next line down one). When you are done, there should be no lines longer than 80 characters.'
print '\n'.join(line.strip() for line in re.findall(r'.{1,80}(?:\s+|$)', s))
Run Code Online (Sandbox Code Playgroud)

结果:

Text Processing. You are tired of seeing lines on your e-mail wrap because
people type lines that are too long for your mail reader application. Create a
program to scan a text file for all lines longer than 80 characters. For each of
the offending lines, find the closest word before 80 characters and break the
line there, inserting the remaining text to the next line (and pushing the
previous next line down one). When you are done, there should be no lines longer
than 80 characters.
Run Code Online (Sandbox Code Playgroud)

您的示例文本是一行,您可能实际上想要使用以下内容:

def split_lines(text):
    lines = text.split('\n')
    regex = re.compile(r'.{1,80}(?:\s+|$)')
    return '\n'.join(s.rstrip() for line in lines for s in regex.findall(line))
Run Code Online (Sandbox Code Playgroud)