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)

head first python菜鸟学习笔记(第四章)的更多相关文章

  1. [Python学习笔记][第四章Python字符串]

    2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...

  2. o'Reill的SVG精髓(第二版)学习笔记——第四章

    第四章:基本形状 4.1线段 SVG可以使用<line>元素画出一条直线段.使用时只需要指定线段起止点的x和y坐标即可.指定坐标时可以不带单位,此时会使用用户坐标,也可以带上单位,如em. ...

  3. python进阶学习笔记(四)--多线程thread

    在使用多线程之前,我们首页要理解什么是进程和线程. 什么是进程? 计算机程序只不过是磁盘中可执行的,二进制(或其它类型)的数据.它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命期.进程( ...

  4. Python基础学习笔记(四)语句

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  5. python基础学习笔记第四天 list 元祖 字典

    一 LIST方法 列表操作包含以下函数:1.cmp(list1, list2):比较两个列表的元素 2.len(list):列表元素个数 3.max(list):返回列表元素最大值 4.min(lis ...

  6. python Django 学习笔记(四)—— 使用MySQL数据库

    1,下载安装MySQLdb类库 http://www.djangoproject.com/r/python-mysql/ 2,修改settings.py 配置数据属性 DATABASES = { 'd ...

  7. 《Python基础教程(第二版)》学习笔记 -> 第四章 字典

    字典是Python中唯一内建的映射类型. 字典中的值并没有特殊的顺序,但是都存储在一个特定的键(Key)里.键可以是数字.字符串甚至是元组. 字典的使用 某些情况下,字典比列表更加适用: 表征游戏棋盘 ...

  8. head first python菜鸟学习笔记(第六章)

    1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...

  9. head first python菜鸟学习笔记(第三章)

    1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...

随机推荐

  1. JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 1

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7703679.html ------------------------------------ ...

  2. 62、django之MTV模型(urls,view)

    今天就进入到python最重要的阶段了django框架,框架就像胶水一样会将我们前面学的所有知识点粘合在一起,所以以前有哪些部分模糊的可以看看前面的随笔.本篇主要介绍djangoMTV模型,视图层之路 ...

  3. java:利用静态字段和构造函数实现已建对象数查询

    问题:使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数. 请写一个类,在任何时候都可以向它查询"你已经创建了多少个对象?". 程序设计思想: 利用静态变量指定一个计数 ...

  4. Linux 源码编译Python 3.6

    Linux 源码编译Python 3.6 1.操作系统以及版本显示 # uname -sr Linux 3.10.0-514.el7.x86_64 # uname -sr Linux 3.10.0-5 ...

  5. Leetcode题解(21)

    62. Unique Paths 题目 分析: 机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能, ...

  6. gops - Go语言程序查看和诊断工具

    想必 Java 的开发者没有不知道或者没用过 jps 这个命令的,这个命令是用来在主机上查看有哪些 Java 程序在运行的. 我刚用 Go 语言程序的时候也很苦恼,我部署在公司服务器上的 Go 程序, ...

  7. Marriage is Stable

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  8. Codeforces 376A. Night at the Museum

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. 0_Simple__matrixMul + 0_Simple__matrixMul_nvrtc

    矩阵乘法,使用一维线程块和共享内存.并且在静态代码和运行时编译两种条件下使用. ▶ 源代码:静态使用 #include <stdio.h> #include <assert.h> ...

  10. ECMAScript 6 proxies

    在ECMAScript 5里面,可以通过(writable 和 configurable)内部属性把属性设置为不可修改和不可删除的,可以通过(Object.preventExtensions() )让 ...