韶关网站建设公司,泉州网站建设定制,百度智能小程序怎么优化排名,自己的电脑做服务器建立网站的方法1、上节课代码中的问题 第四课中介绍了python持久化的基础实例——将数据写入文本文件。 可当我们试着把保存数据的文件读取出来会怎样呢#xff1f; try: with open(man.txt, r) as fman: print(fman.readline())except IOError as err: print(str(err)) 执行时#xff0c;1…1、上节课代码中的问题 第四课中介绍了python持久化的基础实例——将数据写入文本文件。 可当我们试着把保存数据的文件读取出来会怎样呢 try: with open(man.txt, r) as fman: print(fman.readline())except IOError as err: print(str(err)) 执行时1、上节课代码中的问题第四课中介绍了python持久化的基础实例——将数据写入文本文件。可当我们试着把保存数据的文件读取出来会怎样呢try:with open(man.txt, r) as fman:print(fman.readline())except IOError as err:print(str(err))执行时返回一大...串儿字符串。里边包含了man.txt文件中的所有数据。这种未被格式化的存储方式基本上是没什么用的除非你把整个文件当一个字符串读出来然后再去想各种办法解析...2、把即将写入文本文件的数据格式化当然我们可以写出新的代码来实现数据格式化。可第二课中我们曾经创建过一个nester模块里边的print_lol函数就是用来格式化列表的。为什么不把它改造一个直接拿来使用呢不要重复造轮子嘛...OOP吧改造print_lol函数(原代码在第二课中)模块名nester其中包含一个函数print_lol 用于输出一个可能嵌套有其他列表的列表def print_lol(the_list, level0, d\t, indentFalse, file_namesys.stdout):函数print_lol 使用递归的方式输出一个可能嵌套有其他列表的列表参数the_list 是需要输出的列表参数level 是整形表示缩进级别默认值为0参数d 是字符串表示缩进符号默认值为制表符参数indent 是一个布尔值表示是否进行缩进默认为否参数file_name, 为需要写入的文件名默认为不写入任何文件直接输出for each_item in the_list:if isinstance(each_item, list):print_lol(each_item, level1, file_name)else:if indent:for tab_stop in range(level):print(d, end , file file_name)print(each_item, file file_name)改造写入文件的代码块(原代码在第四课中)使用新的print_lol函数来取代print() 这个BIFimport nesterman []other []try:data open(sketch.txt)try:for each_line in data:(role, line_spoken) each_line.split(:, 1)line_spoken line_spoken.strip()if role man:man.append(line_spoken)else:other.append(line_spoken)except ValueError:passdata.close()except IOError as ioerr:print(File Error : str(ioerr))try:with open(man_data.txt, w) as man_file, open(other_data.txt, w) as other_file:nester.print_lol(man, file_name man_file) #把此处的print BIF改为带有格式化列表和写入文件功能的pring_lol函数nester.print_lol(other, file_name other_file)except IOError as ioerr:print(File Error: str(ioerr))如此便可以利用现有的print_lol函数实现把格式化后的列表写入文本文件。