1,p124,错误:NameError: name 'print_lol' is not defined

要想文件内如图显示,需要把调用BIF print()改为调用第二章的nester模块中的print_lol()函数,因此需要导入nester模块。

首先需要修改第二章nester模块中的print_lol()函数并更新该模块,更新方法如图:

然后在sketch.py中调用print_lol()函数。

 '''
修改时间:0919
修改内容:把调用BIF print()改为调用第二章的nester模块中的print_lol()函数,需要导入nester模块
'''
import nester man=[]
other=[]
try:
data=open('sketch.txt')
for each_line in data:
try:
(role,line_spoken)=each_line.split(':',1)
line_spoken=line_spoken.strip()
if role=='Man':
man.append(line_spoken)
elif role=='Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!') try:
with open('man_data.txt','w') as man_file,open('other_data.txt','w') as other_file:
print_lol(man,fh=man_file)
print_lol(other,fh=other_file)
except IOError as err:
print('File Error:'+str(err))

这时会报错:

原因:

调用模块的函数时,格式为“模块名.函数名()”

因此代码更改为:

 '''
修改时间:0919
修改内容:把调用BIF print()改为调用第二章的nester模块中的print_lol()函数,需要导入nester模块
注意:调用print_lol时,需要把模块名加上,nester.print_lol(),而不是直接调用print_lol().
'''
import nester man=[]
other=[]
try:
data=open('sketch.txt')
for each_line in data:
try:
(role,line_spoken)=each_line.split(':',1)
line_spoken=line_spoken.strip()
if role=='Man':
man.append(line_spoken)
elif role=='Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!') try:
with open('man_data.txt','w') as man_file,open('other_data.txt','w') as other_file:
nester.print_lol(man,fh=man_file)
nester.print_lol(other,fh=other_file)
except IOError as err:
print('File Error:'+str(err))

2. pickle模块中dump()和load()的用法

dump()保存数据,load()恢复数据,即下载数据

try:
with open('man_data.txt','wb') as man_file,open('other_data.txt','wb') as other_file:
pickle.dump(man,man_file)
pickle.dump(other,other_file)
except pickle.PickleError as perr:
print('Pickling Error:'+str(perr))
import pickle
import nester
new_name=[]
try:
with open('man_data.txt','rb')as man_file:
new_name=pickle.load(man_file)
except IOError as err:
print('File error:'+str(err))
except pickle.PickleError as perr:
print('Pickling Error:'+str(perr)) nester.print_lol(new_name)

最新文章

  1. GitLab CI持续集成配置方案
  2. python 列表生成式
  3. Python的标准输出
  4. CSS3利用text-shadow属性实现多种效果的文字样式展现
  5. 数据存储与IO(二)
  6. [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
  7. Leetcode 83 Remove Duplicates from Sorted List 链表
  8. c#操作剪切板
  9. Return Negative
  10. 对Gearman中client,worker,jobserver的理解
  11. Fedora上配置一个安全FTP
  12. distance.c
  13. 03-IOSCore - XML及解析、Plist
  14. SP2-1503: 无法初始化 Oracle 调用界面
  15. sqlite不存在记录则插入数据
  16. Java进阶(一)Java内存解析
  17. Pytorch里的CrossEntropyLoss详解
  18. 配置NFS
  19. mongodb系列~mongodb数据迁移
  20. iot-dm异常日志

热门文章

  1. win10 uwp 访问解决方案文件
  2. JavaScript责任链模式
  3. HTML之事件处理程序
  4. sys.argv向脚本中传递参数
  5. oracle导不出空表的解决办法
  6. LINUX 笔记-crontab命令
  7. [hihoCoder]矩形判断
  8. 树莓派.安装Redis环境
  9. appium测试代码nullpoint
  10. IIS下自定义错误页面配置的两种方式(亲测可行)--IIS服务器