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,运行实例代码,没有问题。

实例一:

 1 Python 3.5.2 (default, Nov 17 2016, 17:05:23)
 2 [GCC 5.4.0 20160609] on linux
 3 Type "copyright", "credits" or "license()" for more information.
 4 >>> import os
 5 >>> os.getcwd()
 6 ‘/home/user‘
 7 >>> os.chdir(‘/home/user/project/python_model/HeadFirstPython/chapter3‘)
 8 >>> os.getcwd()
 9 ‘/home/user/project/python_model/HeadFirstPython/chapter3‘
10 >>> man=[]
11 >>> other=[]
12 >>> try:
13     data=open(‘sketch.txt‘)
14     for each_line in data:
15         try:
16             (role,line_spoken)=each_line.split(‘:‘,1)
17             line_spoken=line_spoken.strip()
18             if role==‘Man‘:
19                 man.append(line_spoken)
20             elif role==‘Other Man‘:
21                 other.append(line_spoken)
22         except ValueError:
23             pass
24     data.close()
25 except IOError:
26     print(‘The datafile is missing!‘)
27
28
29 >>> print(man)
30 [‘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!‘]
31 >>> print(other)
32 ["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!"]
33 >>> 

 以写模式打开文件

  使用open()BIF打开磁盘文件时,可以指定访问的模式,open()的帮助文件如下:

  1 help(open)
  2 Help on built-in function open in module io:
  3
  4 open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
  5     Open file and return a stream.  Raise IOError upon failure.
  6
  7     file is either a text or byte string giving the name (and the path
  8     if the file isn‘t in the current working directory) of the file to
  9     be opened or an integer file descriptor of the file to be
 10     wrapped. (If a file descriptor is given, it is closed when the
 11     returned I/O object is closed, unless closefd is set to False.)
 12
 13     mode is an optional string that specifies the mode in which the file
 14     is opened. It defaults to ‘r‘ which means open for reading in text
 15     mode.  Other common values are ‘w‘ for writing (truncating the file if
 16     it already exists), ‘x‘ for creating and writing to a new file, and
 17     ‘a‘ for appending (which on some Unix systems, means that all writes
 18     append to the end of the file regardless of the current seek position).
 19     In text mode, if encoding is not specified the encoding used is platform
 20     dependent: locale.getpreferredencoding(False) is called to get the
 21     current locale encoding. (For reading and writing raw bytes use binary
 22     mode and leave encoding unspecified.) The available modes are:
 23
 24     ========= ===============================================================
 25     Character Meaning
 26     --------- ---------------------------------------------------------------
 27     ‘r‘       open for reading (default)
 28     ‘w‘       open for writing, truncating the file first
 29     ‘x‘       create a new file and open it for writing
 30     ‘a‘       open for writing, appending to the end of the file if it exists
 31     ‘b‘       binary mode
 32     ‘t‘       text mode (default)
 33     ‘+‘       open a disk file for updating (reading and writing)
 34     ‘U‘       universal newline mode (deprecated)
 35     ========= ===============================================================
 36
 37     The default mode is ‘rt‘ (open for reading text). For binary random
 38     access, the mode ‘w+b‘ opens and truncates the file to 0 bytes, while
 39     ‘r+b‘ opens the file without truncation. The ‘x‘ mode implies ‘w‘ and
 40     raises an `FileExistsError` if the file already exists.
 41
 42     Python distinguishes between files opened in binary and text modes,
 43     even when the underlying operating system doesn‘t. Files opened in
 44     binary mode (appending ‘b‘ to the mode argument) return contents as
 45     bytes objects without any decoding. In text mode (the default, or when
 46     ‘t‘ is appended to the mode argument), the contents of the file are
 47     returned as strings, the bytes having been first decoded using a
 48     platform-dependent encoding or using the specified encoding if given.
 49
 50     ‘U‘ mode is deprecated and will raise an exception in future versions
 51     of Python.  It has no effect in Python 3.  Use newline to control
 52     universal newlines mode.
 53
 54     buffering is an optional integer used to set the buffering policy.
 55     Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
 56     line buffering (only usable in text mode), and an integer > 1 to indicate
 57     the size of a fixed-size chunk buffer.  When no buffering argument is
 58     given, the default buffering policy works as follows:
 59
 60     * Binary files are buffered in fixed-size chunks; the size of the buffer
 61       is chosen using a heuristic trying to determine the underlying device‘s
 62       "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
 63       On many systems, the buffer will typically be 4096 or 8192 bytes long.
 64
 65     * "Interactive" text files (files for which isatty() returns True)
 66       use line buffering.  Other text files use the policy described above
 67       for binary files.
 68
 69     encoding is the name of the encoding used to decode or encode the
 70     file. This should only be used in text mode. The default encoding is
 71     platform dependent, but any encoding supported by Python can be
 72     passed.  See the codecs module for the list of supported encodings.
 73
 74     errors is an optional string that specifies how encoding errors are to
 75     be handled---this argument should not be used in binary mode. Pass
 76     ‘strict‘ to raise a ValueError exception if there is an encoding error
 77     (the default of None has the same effect), or pass ‘ignore‘ to ignore
 78     errors. (Note that ignoring encoding errors can lead to data loss.)
 79     See the documentation for codecs.register or run ‘help(codecs.Codec)‘
 80     for a list of the permitted encoding error strings.
 81
 82     newline controls how universal newlines works (it only applies to text
 83     mode). It can be None, ‘‘, ‘\n‘, ‘\r‘, and ‘\r\n‘.  It works as
 84     follows:
 85
 86     * On input, if newline is None, universal newlines mode is
 87       enabled. Lines in the input can end in ‘\n‘, ‘\r‘, or ‘\r\n‘, and
 88       these are translated into ‘\n‘ before being returned to the
 89       caller. If it is ‘‘, universal newline mode is enabled, but line
 90       endings are returned to the caller untranslated. If it has any of
 91       the other legal values, input lines are only terminated by the given
 92       string, and the line ending is returned to the caller untranslated.
 93
 94     * On output, if newline is None, any ‘\n‘ characters written are
 95       translated to the system default line separator, os.linesep. If
 96       newline is ‘‘ or ‘\n‘, no translation takes place. If newline is any
 97       of the other legal values, any ‘\n‘ characters written are translated
 98       to the given string.
 99
100     If closefd is False, the underlying file descriptor will be kept open
101     when the file is closed. This does not work when a file name is given
102     and must be True in that case.
103
104     A custom opener can be used by passing a callable as *opener*. The
105     underlying file descriptor for the file object is then obtained by
106     calling *opener* with (*file*, *flags*). *opener* must return an open
107     file descriptor (passing os.open as *opener* results in functionality
108     similar to passing None).
109
110     open() returns a file object whose type depends on the mode, and
111     through which the standard file operations such as reading and writing
112     are performed. When open() is used to open a file in a text mode (‘w‘,
113     ‘r‘, ‘wt‘, ‘rt‘, etc.), it returns a TextIOWrapper. When used to open
114     a file in a binary mode, the returned class varies: in read binary
115     mode, it returns a BufferedReader; in write binary and append binary
116     modes, it returns a BufferedWriter, and in read/write mode, it returns
117     a BufferedRandom.
118
119     It is also possible to use a string or bytearray as a file for both
120     reading and writing. For strings StringIO can be used like a file
121     opened in a text mode, and for bytes a BytesIO can be used like a file
122     opened in a binary mode.

View help

 实例二:

 1 import os
 2 os.getcwd()
 3 os.chdir(‘/home/user/project/python_model/HeadFirstPython/chapter3‘)
 4 man = []
 5 other = []
 6 try:
 7  data = open(‘sketch.txt‘)
 8  for each_line in data:
 9   try:
10    (role,line_spoken) = each_line.split(‘:‘,1)
11    line_spoken = line_spoken.strip()
12    if role == ‘Man‘:
13     man.append(line_spoken)
14    elif role == ‘Other Man‘:
15     other.append(line_spoken)
16   except ValueError:
17    pass
18  data.close()
19 except IOError:
20  print(‘The datafile is missing!‘)
21 try:
22  man_file = open(‘man_data.txt‘,‘w‘) # open a new file man_data.txt in-mode ‘w‘
23  other_file = open(‘other_data.txt‘,‘w‘)# if the file don‘t exist then creat it.
24  print(man,file=man_file)# write man data into man_file.txt
25  print(other,file=other_file)# write other data into other_file.txt
26  man_file.close()# close man_file
27  other_file.close()# close other_file
28 except IOError:
29  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)

  

时间: 2024-11-05 13:39:14

Python(Head First)学习笔记:四的相关文章

如何输出格式化的字符串(学习笔记四)

如何输出格式化的字符串(学习笔记四) 我们经常会输出类似 '亲爱的xxx你好!你xx月的话费是xx,余额是xx' 之类的字符串,而xxx的内容都是根据变量变化的,所以,需要一种简便的格式化字符串的方式. 在Python中,采用的格式化方式和C语言是一致的,用 % 实现,举例如下: >>> 'Hello, %s' % ('world') 'Hello, world' 截图如下: 注: (1)红线上的百分号,两边可有空格也可无: (2)对于只有一个变量的情况,我们可以将'world'外的括号

Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能

Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双向的所以我们想动态的实现多语言切换很是方便今天我做一个小demo给大家提供一个思路 先看一下效果 点击英文  变成英文状态点chinese就会变成中文                          源码的下载地址在文章的最下边 多语言用的是资源文件建

代码管理工具 --- git的学习笔记四《重新整理git(1)》

1.创建版本库 mkdir  创建目录 cd  地址,到该地址下 pwd 显示当前目录 1.创建目录 $ mkdir startGit $ cd startGit $ pwd 显示当前目录 或者cd到桌面,然后再创建目录 2.初始化版本库 $ git init 初始化仓库 提示信息:Initialized empty Git repository in /Users/xingzai/Desktop/startGit/.git/ 建立一个空的git仓库在/Users/xingzai/Desktop

Linux学习笔记四:Linux的文件搜索命令

1.文件搜索命令  which 语法:which [命令名称] 范例:$which ls  列出ls命令所在目录 [[email protected] ~]$ which ls alias ls='ls --color=auto' /bin/ls 另外一个命令:whereis [名称名称],也可以列出命令所在目录. [[email protected] ~]$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/ma

小猪的数据结构学习笔记(四)

小猪的数据结构学习笔记(四) 线性表之静态链表 --转载请注明出处:coder-pig 本章引言: 在二,三中中我们分别学习了顺序表中的线性表与单链表,线性表有点类似于 我们前面所学的数组,而单链表使用的最多的是指针,这里问个简单的问题, 如果是在以前没有指针的话,前辈先人们怎么实现单链表呢?大家思考下! 没有指针,那么用什么来代替呢?前辈先人们非常机智,想出了使用下标+游标的方式 来实现单链表的效果!也就是今天要讲的--静态链表! 当然你也可以直接跳过本章,因为有了单链表就没有必要用静态链表了

Swift学习笔记四:数组和字典

最近一个月都在专心做unity3d的斗地主游戏,从早到晚,最后总算是搞出来了,其中的心酸只有自己知道.最近才有功夫闲下来,还是学习学习之前的老本行--asp.net,现在用.net做项目流行MVC,而不是之前的三层,既然技术在更新,只能不断学习,以适应新的技术潮流! 创建MVC工程 1.打开Visual studio2012,新建MVC4工程 2.选择工程属性,创建MVC工程 3.生成工程的目录 App_Start:启动文件的配置信息,包括很重要的RouteConfig路由注册信息 Conten

NLTK学习笔记(四):自然语言处理的一些算法研究

自然语言处理中算法设计有两大部分:分而治之 和 转化 思想.一个是将大问题简化为小问题,另一个是将问题抽象化,向向已知转化.前者的例子:归并排序:后者的例子:判断相邻元素是否相同(与排序). 这次总结的自然语言中常用的一些基本算法,算是入个门了. 递归 使用递归速度上会受影响,但是便于理解算法深层嵌套对象.而一些函数式编程语言会将尾递归优化为迭代. 如果要计算n个词有多少种组合方式?按照阶乘定义:n! = n*(n-1)*...*1 def func(wordlist): length = le

Android学习笔记四:添加Source

问题描述 Source not foundThe JAR file D:\.....\sdk\platforms\android-20\android.jar has no source attachment. 问题原因及解决办法 1. 使用SDK Manager下载最新版本的Sources for Android SDK 一般文件下载目录默认在SDK下的sources文件中即 \adt-bundle-windows-x86_64-20130522\sdk\sources\android-20

【Unity 3D】学习笔记四十二:粒子特效

粒子特效 粒子特效的原理是将若干粒子无规则的组合在一起,来模拟火焰,爆炸,水滴,雾气等效果.要使用粒子特效首先要创建,在hierarchy视图中点击create--particle system即可 粒子发射器 粒子发射器是用于设定粒子的发射属性,比如说粒子的大小,数量和速度等.在创建完粒子对象后,在右侧inspector视图中便可以看到所有的粒子属性: emit:是否是使用粒子发射器. min size:粒子最小尺寸. max size:粒子最大尺寸. min energy:粒子的最小生命周期

WEB前端学习笔记 四

接上一篇,web学习笔记 四,在此感谢您对此篇笔记的认可,但转发时请注明文章出自网知博学. 2.0  html的语法格式 html的标签要写在尖括号中 :<> 在在英文输入法状态下,按住shift键然后再按它左侧的尖括号就可了, 先学习一个简单的h1标签,是个标题标签,在html中这样写: <h1>我在h1标签中,我就是标题</h1> 那么h1标签中所包裹的文字,就标记成标题了.通过浏览器的解析后在页面上显示出来的效果就是字体加粗,加黑,和word中的标题性质一样! 大