head first python菜鸟学习笔记(第四章)
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菜鸟学习笔记(第四章)的更多相关文章
- [Python学习笔记][第四章Python字符串]
2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...
- o'Reill的SVG精髓(第二版)学习笔记——第四章
第四章:基本形状 4.1线段 SVG可以使用<line>元素画出一条直线段.使用时只需要指定线段起止点的x和y坐标即可.指定坐标时可以不带单位,此时会使用用户坐标,也可以带上单位,如em. ...
- python进阶学习笔记(四)--多线程thread
在使用多线程之前,我们首页要理解什么是进程和线程. 什么是进程? 计算机程序只不过是磁盘中可执行的,二进制(或其它类型)的数据.它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命期.进程( ...
- Python基础学习笔记(四)语句
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...
- python基础学习笔记第四天 list 元祖 字典
一 LIST方法 列表操作包含以下函数:1.cmp(list1, list2):比较两个列表的元素 2.len(list):列表元素个数 3.max(list):返回列表元素最大值 4.min(lis ...
- python Django 学习笔记(四)—— 使用MySQL数据库
1,下载安装MySQLdb类库 http://www.djangoproject.com/r/python-mysql/ 2,修改settings.py 配置数据属性 DATABASES = { 'd ...
- 《Python基础教程(第二版)》学习笔记 -> 第四章 字典
字典是Python中唯一内建的映射类型. 字典中的值并没有特殊的顺序,但是都存储在一个特定的键(Key)里.键可以是数字.字符串甚至是元组. 字典的使用 某些情况下,字典比列表更加适用: 表征游戏棋盘 ...
- head first python菜鸟学习笔记(第六章)
1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...
- head first python菜鸟学习笔记(第三章)
1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...
随机推荐
- win10 UWP 获取系统信息
获取系统信息 Windows.System.Profile.AnalyticsVersionInfo analyticsVersion = Windows.System.Profile.Analyti ...
- 通过xrdp服务实现windows远程桌面连接树莓派
如题:通过xrdp服务实现windows远程桌面连接树莓派 受同学影响,最近接触到了树莓派,又加上自己技痒想试一下这个小东西究竟能做什么,所以开始了树莓派学习之旅. 正题开始-xrdp实现window ...
- fio2.1.10--HOWTO
1.0 Overview and history ------------------------ fio was originally written to save me the hassl ...
- 【转】CPU与内存的那些事
下面是网上看到的一些关于内存和CPU方面的一些很不错的文章. 整理如下: 转: CPU的等待有多久? 原文标题:What Your Computer Does While You Wait 原文地址: ...
- [板子]Floyd&Dijkstra
谨以此笔记记录jjw高三党四个月学习NOI的历程..如转载请标记出处 Floyd算法: 默认是业界最短路最简单的写法,并且只有五行.时间复杂度为O(N3),空间复杂度为O(N2). ;k<=n; ...
- c++ 类覆盖方法中的协变返回类型
c++ 类覆盖方法中的协变返回类型 在C++中,只要原来的返回类型是指向类的指针或引用,新的返回类型是指向派生类的指针或引用,覆盖的方法就可以改变返回类型.这样的类型称为协变返回类型(Covarian ...
- C. New Year and Rating
C. New Year and Rating time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Android 开发笔记___SD卡基本操作
package com.example.alimjan.hello_world; /** * Created by alimjan on 7/5/2017. */ import android.ann ...
- html2cavans
简介:http://www.jianshu.com/p/6a07e974a7e8 下载:https://github.com/niklasvh/html2canvas/releases C#代理git ...
- cookie 子域名可以读父域名中的cookie
cookie 子域名可以读父域名中的cookie 如在 .ping.com域下注入cookie,则该子域下的网页如p1.ping.com.p2.ping.com 都能读取到cookie信息 path的 ...