我正在写一个程序,它限制每一行的长度。在
这就是我到目前为止所得到的,我几乎完成了,但我仍然需要删减每一行,但我不明白。在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)
编辑:
^{pr2}$
输出应该是什么的一个例子如下。
如果指定的文件包含此文本:hgytuinghdt #here the length is 11
ughtnjuiknshfyth #here the length is 16
nmjhkaiolgytuhngjuin #here the length is 20
l长度被指定为15,则应打印出:hgytuinghdt
ughtnjuiknshfyt
h
nmjhkaiolgytuhng
juin