Python(Head First)学习笔记:四
4 持久存储:文件存储、读写
数据保存到文件:在学习的过程中出现了一个问题,老是报一个错:SyntaxError: invalid syntax;
这个是语法错误,后来搜了下才知道是python2.7和python3.5并不兼容,因为之前一直是在ubuntu的终端里
写这些简单的实例,后来程序稍微大点就不方便了,就安装了idle,用命令:sudo apt-get install idle,安装完启动后,
载入python文件,然后运行发现是python2.7,然后逐行运行,发现报错,而之前这些代码都是没问题的,后来重新安
装idle3,命令:sudo apt-get install idle3,然后启动:idle3,运行实例代码,没有问题。
实例一:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.getcwd()
'/home/user'
>>> os.chdir('/home/user/project/python_model/HeadFirstPython/chapter3')
>>> os.getcwd()
'/home/user/project/python_model/HeadFirstPython/chapter3'
>>> 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!') >>> print(man)
['Is this the right room for an argument?', "No you haven't!", 'When?', "No you didn't!", "You didn't!", 'You did not!', 'Ah! (taking out his wallet and paying) Just the five minutes.', 'You most certainly did not!', "Oh no you didn't!", "Oh no you didn't!", "Oh look, this isn't an argument!", "No it isn't!", "It's just contradiction!", 'It IS!', 'You just contradicted me!', 'You DID!', 'You did just then!', '(exasperated) Oh, this is futile!!', 'Yes it is!']
>>> print(other)
["I've told you once.", 'Yes I have.', 'Just now.', 'Yes I did!', "I'm telling you, I did!", "Oh I'm sorry, is this a five minute argument, or the full half hour?", 'Just the five minutes. Thank you.', 'Anyway, I did.', "Now let's get one thing quite clear: I most definitely told you!", 'Oh yes I did!', 'Oh yes I did!', 'Yes it is!', "No it isn't!", 'It is NOT!', "No I didn't!", 'No no no!', 'Nonsense!', "No it isn't!"]
>>>
以写模式打开文件
使用open()BIF打开磁盘文件时,可以指定访问的模式,open()的帮助文件如下:
help(open)
Help on built-in function open in module io: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Open file and return a stream. Raise IOError upon failure. file is either a text or byte string giving the name (and the path
if the file isn't in the current working directory) of the file to
be opened or an integer file descriptor of the file to be
wrapped. (If a file descriptor is given, it is closed when the
returned I/O object is closed, unless closefd is set to False.) mode is an optional string that specifies the mode in which the file
is opened. It defaults to 'r' which means open for reading in text
mode. Other common values are 'w' for writing (truncating the file if
it already exists), 'x' for creating and writing to a new file, and
'a' for appending (which on some Unix systems, means that all writes
append to the end of the file regardless of the current seek position).
In text mode, if encoding is not specified the encoding used is platform
dependent: locale.getpreferredencoding(False) is called to get the
current locale encoding. (For reading and writing raw bytes use binary
mode and leave encoding unspecified.) The available modes are: ========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
========= =============================================================== The default mode is 'rt' (open for reading text). For binary random
access, the mode 'w+b' opens and truncates the file to 0 bytes, while
'r+b' opens the file without truncation. The 'x' mode implies 'w' and
raises an `FileExistsError` if the file already exists. Python distinguishes between files opened in binary and text modes,
even when the underlying operating system doesn't. Files opened in
binary mode (appending 'b' to the mode argument) return contents as
bytes objects without any decoding. In text mode (the default, or when
't' is appended to the mode argument), the contents of the file are
returned as strings, the bytes having been first decoded using a
platform-dependent encoding or using the specified encoding if given. 'U' mode is deprecated and will raise an exception in future versions
of Python. It has no effect in Python 3. Use newline to control
universal newlines mode. buffering is an optional integer used to set the buffering policy.
Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
line buffering (only usable in text mode), and an integer > 1 to indicate
the size of a fixed-size chunk buffer. When no buffering argument is
given, the default buffering policy works as follows: * Binary files are buffered in fixed-size chunks; the size of the buffer
is chosen using a heuristic trying to determine the underlying device's
"block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
On many systems, the buffer will typically be 4096 or 8192 bytes long. * "Interactive" text files (files for which isatty() returns True)
use line buffering. Other text files use the policy described above
for binary files. encoding is the name of the encoding used to decode or encode the
file. This should only be used in text mode. The default encoding is
platform dependent, but any encoding supported by Python can be
passed. See the codecs module for the list of supported encodings. errors is an optional string that specifies how encoding errors are to
be handled---this argument should not be used in binary mode. Pass
'strict' to raise a ValueError exception if there is an encoding error
(the default of None has the same effect), or pass 'ignore' to ignore
errors. (Note that ignoring encoding errors can lead to data loss.)
See the documentation for codecs.register or run 'help(codecs.Codec)'
for a list of the permitted encoding error strings. newline controls how universal newlines works (it only applies to text
mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
follows: * On input, if newline is None, universal newlines mode is
enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
these are translated into '\n' before being returned to the
caller. If it is '', universal newline mode is enabled, but line
endings are returned to the caller untranslated. If it has any of
the other legal values, input lines are only terminated by the given
string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any '\n' characters written are
translated to the system default line separator, os.linesep. If
newline is '' or '\n', no translation takes place. If newline is any
of the other legal values, any '\n' characters written are translated
to the given string. If closefd is False, the underlying file descriptor will be kept open
when the file is closed. This does not work when a file name is given
and must be True in that case. A custom opener can be used by passing a callable as *opener*. The
underlying file descriptor for the file object is then obtained by
calling *opener* with (*file*, *flags*). *opener* must return an open
file descriptor (passing os.open as *opener* results in functionality
similar to passing None). open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed. When open() is used to open a file in a text mode ('w',
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
a file in a binary mode, the returned class varies: in read binary
mode, it returns a BufferedReader; in write binary and append binary
modes, it returns a BufferedWriter, and in read/write mode, it returns
a BufferedRandom. It is also possible to use a string or bytearray as a file for both
reading and writing. For strings StringIO can be used like a file
opened in a text mode, and for bytes a BytesIO can be used like a file
opened in a binary mode.
View help
实例二:
import os
os.getcwd()
os.chdir('/home/user/project/python_model/HeadFirstPython/chapter3')
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:
man_file = open('man_data.txt','w') # open a new file man_data.txt in-mode 'w'
other_file = open('other_data.txt','w')# if the file don't exist then creat it.
print(man,file=man_file)# write man data into man_file.txt
print(other,file=other_file)# write other data into other_file.txt
man_file.close()# close man_file
other_file.close()# close other_file
except IOError:
print('File error')
注:发生异常后文件会保持打开
为了解决发生异常文件没有自动关闭的问题,引入finally。
用finally扩展try
在实例二的最后增加:
finally:
man_file.close()
other_file.close()
在python中字符串是不可变的,因为永远不知道还有哪些变量指向某个特定的字符串;
尽管可以为Python变量赋数值,但实际上变量并不包含所赋的数据;
此外,还有元组也不可以改变,即:不可改变的列表;
所有数值类型也是不可变的。
知道错误类型还不够
如果想知道产生错误的具体原因,就需要添加异常处理捕获机制,如下:
假设现在要打开一个文件:missing.txt,但这个文件并不存在,如下代码:
try:
data=open('missing.txt')
print(data.readline(),end='')
except IOError:
print('File error')
finally:
if 'data' in locals():
data.close()
继续改进:
except IOError as err: #为异常对象起一个名
print('File error: ' + str(err)) #然后作为错误消息的一部分
然后运行,结果是:File error:[Errno 2] No such file or directory: 'missing.txt';
但是如果代码量大了,这种逻辑处理方法会很麻烦,这样引入with。
用with处理文件
使用以下代码可以替代上面的try/except/finally代码:
try:
with open('its.txt',"w") as data:
print("It's...",file=data)
except IOError as err:
print('File error:' + str(err))
注:使用with时,不需要操心关闭打开文件,Python解释器会自动处理;
其实,with语句使用了一种名叫:上下文管理协议(context management protocol)的Python技术。
接下来修改第二章笔记中的print_lol()函数
在Python中,标准输出是:sys.stdout,可以从标准库sys模块导入。
实例三
对函数print_lol做修改
def print_lol(the_list,indent=False,level=0,fh=sys.stdout ):
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item,indent,level+1,fh)
else:
for tab_stop in range(level):
print("\t" *level,end='',file=fh)
print(each_item,file=fh)
不知道为什么,print_lol函数在添加了第四个参数fh=sys.stdout后,用import sys及import nester后报错:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
import nester
File "/home/user/project/python_model/nester/nester.py", line 1, in <module>
def print_lol(the_list,indent=False,level=0,fh=sys.stdout ):
NameError: name 'sys' is not defined
上网查找也没有解决这个问题,挺郁闷的,已经卡住两天了,先跳过去了。。。
定制代码剖析
“腌制”数据
Python提供了一个标准库,名为:pickle,它可以保存和加载几乎任何Python数据对象,包括列表。
可以把“腌制”数据存储到磁盘,放到数据库或者通过网络传输到另一台计算机上。
用dump保存,用load恢复
使用pickle很简单,只需导入模块:import pickle;
用dump()保存数据;
用load()恢复数据;
注:处理“腌制数据”,唯一的要求是,必须以二进制访问模式打开这些文件。
如果出问题了呢?
腌制或解除数据腌制时如果出了问题,pickle模块会产生一个PickleError类型的异常。
实例四:文件数据的腌制和恢复
>>> print(man)
['Is this the right room for an argument?', "No you haven't!", 'When?', "No you didn't!", "You didn't!", 'You did not!', 'Ah! (taking out his wallet and paying) Just the five minutes.', 'You most certainly did not!', "Oh no you didn't!", "Oh no you didn't!", "Oh look, this isn't an argument!", "No it isn't!", "It's just contradiction!", 'It IS!', 'You just contradicted me!', 'You DID!', 'You did just then!', '(exasperated) Oh, this is futile!!', 'Yes it is!']
>>> try:
man_file=open('man_data.txt','w')
other_file=open('other_data.txt','w')
print(man,file=man_file)
print(other,file=other_file)
man_file.close()
other_file.close()
except IOError:
print('File error') >>> import pickle
>>> try:
with open('man_data1.txt','wb')as man_file:
pickle.dump(man,man_file)
except IOError as err:
print('File error:'+str(err))
except pickle.PickleError as perr:
print('Pickling error:'+str(perr)) >>> new_man=[]
>>> try:
with open('man_data1.txt','rb')as man_file:
new_man = pickle.load(man_file)
except IOError as err:
print('File error:'+str(err))
except pickle.PickleError as perr:
print('Pickling error:'+str(perr)) >>> import nester01
>>> nester01.print_lol(new_man)
Is this the right room for an argument?
No you haven't!
When?
No you didn't!
You didn't!
You did not!
Ah! (taking out his wallet and paying) Just the five minutes.
You most certainly did not!
Oh no you didn't!
Oh no you didn't!
Oh look, this isn't an argument!
No it isn't!
It's just contradiction!
It IS!
You just contradicted me!
You DID!
You did just then!
(exasperated) Oh, this is futile!!
Yes it is!
最后,显示数据的第一行和最后一行:
>>> print(new_man[0]) #显示第一行
Is this the right room for an argument?
>>> print(new_man[-1]) #显示最后一行
Yes it is!
总结
使用Pickle的通用文件I/O才是上策!嘿嘿~
让Python去负责文件I/O的细节,这样把关注重点放在代码的实际作用;
利用Python处理、保存和恢复列表中的数据,现在已经有一套可行、可靠的机制,
本章主要用到的方法有:
strip():可以从字符串中去除不想要的空白符;
print():BIF的参数控制,将数据发送、保存到相应地址;
finally:最终会执行的语句;
except:会传入一个异常对象并通过as赋值到一个标识符;
str():BIF可以用来访问任何数据对象的串表示;
locals():返回当前作用域的变量集合;
in:操作符用于检查成员关系;
+:连接两个字符串或两个数字相加;
with:自动处理已有打开文件的关闭工作,即使出现异常也会执行;
sys.stdout:Python中的标准输出,需要加载sys模块;
pickle模块:高效的将Python数据对象保存到磁盘(二进制)及从磁盘恢复,包括dump()保存和load()恢复。
-------------------------------------------The End of Fourth Chapter-------------------------------------------
Python(Head First)学习笔记:四的更多相关文章
- python3.4学习笔记(四) 3.x和2.x的区别,持续更新
python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...
- 官网实例详解-目录和实例简介-keras学习笔记四
官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras 版权声明: ...
- openresty 学习笔记四:连接mysql和进行相关操作
openresty 学习笔记四:连接mysql和进行相关操作 毕竟redis是作为缓存,供程序的快速读写,虽然reidis也可以做持久化保存,但还是需要一个做数据存储的数据库.比如首次查询数据在red ...
- C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻
前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- java之jvm学习笔记四(安全管理器)
java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...
- python网络爬虫学习笔记
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(四) indigo devices
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Python Built-in Function 学习笔记
Python Built-in Function 学习笔记 1. 匿名函数 1.1 什么是匿名函数 python允许使用lambda来创建一个匿名函数,匿名是因为他不需要以标准的方式来声明,比如def ...
随机推荐
- 内存泄漏排查之:Show me your Memory
java 语言有个神奇的地方,那就是你时不时会去关注下内存.(当然了,任何牛逼的同学都应该关注内存) 今天我们就来这么场景吧:某应用运行了一段时间后,ecs监控报警了,内存比较高了,怎么办?随着时间的 ...
- 转载 | embed用法(网站中视频、音频的添加)
网站中添加视频: <embed src="http://player.video.qiyi.com/390cf6c74450e4c70b7bd2d883169914/0/0/w_19r ...
- 【数据结构】线段树(Segment Tree)
假设我们现在拿到了一个非常大的数组,对于这个数组里面的数字要反复不断地做两个操作. 1.(query)随机在这个数组中选一个区间,求出这个区间所有数的和. 2.(update)不断地随机修改这个数组中 ...
- Kafka 系列(四)—— Kafka 消费者详解
一.消费者和消费者群组 在 Kafka 中,消费者通常是消费者群组的一部分,多个消费者群组共同读取同一个主题时,彼此之间互不影响.Kafka 之所以要引入消费者群组这个概念是因为 Kafka 消费者经 ...
- ASP.NET Core MVC 之区域(Area)
区域(Area)是一个 ASP.NET MVC 功能,用于将相关功能组织为一个单独的命名空间(用于路由)和文件结构(用于视图).使用区域通过向控制器和操作添加 一个路由参数(area)来创建用于路由目 ...
- Go-如何读取yaml,json,ini等配置文件
1. json使用 JSON 应该比较熟悉,它是一种轻量级的数据交换格式.层次结构简洁清晰 ,易于阅读和编写,同时也易于机器解析和生成. 创建 conf.json: { "enabled&q ...
- 通知神器——java调用钉钉群自定义机器人
创建群自定义机器人 在指定钉钉群(或者随便拉两个人建个群,然后把别人T出去)的群设置里选择 群机器人 -> 自定义,如图: 然后,添加机器人,设置名字,添加成功时如下图: 其中webhook非常 ...
- Scala 系列(十)—— 函数 & 闭包 & 柯里化
一.函数 1.1 函数与方法 Scala 中函数与方法的区别非常小,如果函数作为某个对象的成员,这样的函数被称为方法,否则就是一个正常的函数. // 定义方法 def multi1(x:Int) = ...
- C#开发BIMFACE系列4 服务端API之源上传文件
在注册成为BIMFACE的应用开发者后,要能在浏览器里浏览你的模型或者获取你模型内的BIM数据, 首先需要把你的模型文件上传到BIMFACE.根据不同场景,BIMFACE提供了丰富的文件相关的接口. ...
- CodeForces 989D
题意略. 思路: 可以看成是所有的云彩照常运动,而月亮在跑.只要两个云彩相交后,在分离前月亮能赶到,就算是符合题意的. 可以知道,两个相隔越远的相向运动地云彩是越有可能符合题意的,因为它们相遇所用时间 ...