python 创建文本文件指定每行字符数_在将原始输入打印到文本文件时,如何限制每行的字符数?...

我正在尝试编写一个报表创建脚本。简单地说,我让用户通过一些raw_input()s提交字符串。这些字符串被分配给全局变量,当它们完成时,我需要脚本来打印字符串,但每行只能打印80个字符。我查看了textwrap模块,并四处寻找其他人。但我只发现有人试图限制脚本中从原始输入或预先存在的文件中打印的字符,而从不尝试打印到新文件中。这里有一些代码,基本上是我要做的事情的简短版本。在

代码如下:

在def start():

global file_name

file_name = raw_input("\nPlease Specify a filename:\n>>> ")

print "The filename chosen is: %r" % file_name

create_report()

note_type()

def create_report():

global new_report

new_report = open(file_name, 'w')

print "Report created as: %r" % file_name

new_report.write("Rehearsal Report\n")

note_type()

def note_type():

print "\nPlease select which type of note you wish to make."

print """

1. Type1

2. Print

"""

answer = raw_input("\n>>> ")

if answer in "1 1. type1 Type1 TYPE1":

type1_note()

elif answer in "2 2. print Print PRINT":

print_notes()

else:

print "Unacceptable Response"

note_type()

def type1_note():

print "Please Enter your note:"

global t1note_text

t1note_text = raw_input(">>> ")

print "\nNote Added."

note_type()

def print_notes():

new_report.write("\nType 1: %r" % t1note_text)

new_report.close

print "Printed. Goodbye!"

exit(0)

start()

这是我的终端输入

^{pr2}$

唯一的问题是,当我打开文件(test3.txt)时,lorem ipsum的整个段落都打印到一行。像这样:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at dignissim diam. Donec aliquam consectetur pretium. Sed ac sem eu nulla tincidunt accumsan. Praesent vel velit odio. Donec porta mauris ut eros bibendum consequat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer adipiscing nibh in turpis placerat non interdum magna convallis. Phasellus porta mauris at nibh laoreet ac vulputate elit semper.

有没有人建议让textwrap在文件中每行打印80个字符?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值