Python textwrap和重置everyline后的计数

Python textwrap和重置everyline后的计数,python,python-3.x,computer-science,Python,Python 3.x,Computer Science,大家好,我有一个问题,这个问题是我需要重置计数后,在文件中的每一行,我把一个评论,这样你可以看到我想重置计数 假设程序在每个指定的行长度之后剪切每一行 def insert_newlines(string, afterEvery_char): lines = [] for i in range(0, len(string), afterEvery_char): lines.append(string[i:i+afterEvery_char]) st

大家好,我有一个问题,这个问题是我需要重置计数后,在文件中的每一行,我把一个评论,这样你可以看到我想重置计数

假设程序在每个指定的行长度之后剪切每一行

def insert_newlines(string, afterEvery_char):
    lines = []
    for i in range(0, len(string), afterEvery_char):
        lines.append(string[i:i+afterEvery_char])
        string[:afterEvery_char] #i want to reset here to the beginning of every line to start count over
    print('\n'.join(lines))

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

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

    print("\nYour file contains the following text: \n" + file + "\n\n") # Prints original File to screen
    print("Here is your output formated to a max of", lineLength, "characters per line: ")

    insert_newlines(file, lineLength)
main()
线切割后应该是这样的

andhsytghfydhtb
cndhg
andhsytghfydhtb
cndhg
andhsytghfydhtb
cndhg

我想在文件中的每一行之后重置计数。

我不理解这里的问题,代码似乎工作正常:

def insert_newlines(string, afterEvery_char):
    lines = []
    # if len(string) is 100 and afterEvery_char is 10
    # then i will be equal to 0, 10, 20, ... 90
    # in lines we'll have [string[0:10], ..., string[90:100]] (ie the entire string)
    for i in range(0, len(string), afterEvery_char):
        lines.append(string[i:i+afterEvery_char])
        # resetting i here won't have any effect whatsoever
    print('\n'.join(lines))


>>> insert_newlines('Beautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\n..', 10)
Beautiful
is better
than ugly.

Explicit
is better
than impli
cit.
Simpl
e is bette
r than com
plex.
..

这不是您想要的吗?

我不确定是否理解您的问题,但从您的评论来看,您似乎只是想将输入字符串(文件)剪切到行长。这已经在insert_newlines()中完成了,不需要在其中添加注释的行

但是,如果要输出以换行符结尾的字符串,且长度不应超过lineLength,则可以如下方式读取文件:

lines = []
while True:
    line = openFile.readline(lineLength)
    if not line:
        break
    if line[-1] != '\n':
        line += '\n'
    lines.append(line)
print(''.join(lines))
或者:

lines = []
while True:
    line = openFile.readline(lineLength)
    if not line:
        break
    lines.append(line.rstrip('\n'))
print('\n'.join(lines))

当从文件中读取行时,它不能正常工作。当你把这样的字符串放入函数中,它似乎可以工作,但当它从文件中读取时,它会剪切ines奇怪的东西我想要的是,一旦它剪切第一行,它就会对后面的每一行执行相同的操作。例如,如果第1-3行在切割第一行时各有20个字符,则将有15个字符。之后的行将有5个字符(显然),然后下一行有20个字符,将在15个字符后再次剪切,依此类推,文件中没有“\n”。它只是一个每行都有字符的文件,当我尝试此解决方案时,它不起作用,“\n”表示新行字符,如果它包含多个文本行,则必须在文件中。但我确实在没有测试的情况下写了一个小错误。现在试试:)你是如何测试这个的?当我这样做的时候,我没有得到任何回报。屏幕最终不会打印任何内容。我抓起你刚刚写的东西,把它放在写着“打印”的行下(“这是你的输出格式,最大为”,行长,“每行字符数:”)“……当然,我去掉了插入新行功能是的,这是它应该去的地方,而不是
插入新行(文件,行长)
。另外,您可以删除行
file=openFile.read()
。是的,我这样做了,只是我需要file=openFile.read(),因为我在打印格式化文件之前先打印原始文件。但是如果你抓住了整个代码并使用它,我最终什么也得不到。
lines = []
while True:
    line = openFile.readline(lineLength)
    if not line:
        break
    lines.append(line.rstrip('\n'))
print('\n'.join(lines))