随候鸟南飞对《Python核心编程(第二版)》的笔记(4)

Python核心编程(第二版)
  • 书名: Python核心编程(第二版)
  • 作者: [美]Wesley J. Chun(陳仲才)
  • 页数: 654
  • 出版社: 人民邮电出版社
  • 出版年: 2008-06
  • 第10页
    有一个更好的方案,许多Unix系统有一个命令叫env,位于/bin或/usr/bin中。它会帮你在系统搜索路径中找到python解释器。如果你的系统拥有env,你的启动行就可以改为下面这样。
    引自第10页
    #!/usr/bin/env python
    2011-02-16 21:47:13 回应
  • 第21页

    下划线(_)在解释器中有特别的含义,表示最后一个表达式的值。

    >>> s = 'hello world'
    >>> print s
    hello world
    >>> _
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name '_' is not defined
    >>> s
    'hello world'
    >>> _
    'hello world'
    print语句也支持将输出重定向到文件。这个特性是从Python2.0开始新增的。符号>>用来重定向输出
    引自第21页

    输出重定向到日志文件的例子:

    >>> logfile = open('/tmp/mylog.txt', 'a')
    >>> print >> logfile, 'Fatal error: invalid input!'
    >>> logfile.close()

    2015-03-30 14:27:47 1人喜欢 回应
  • 第29页

    print语句最后添加一个逗号(,)可以让多个print输出在同一行,输出元素之间会自动添加一个空格

    >>> print 'hello',;print 'world'
    hello world
    2011-02-16 22:18:16 1回应
  • 第31页

    之前读文件数据的时候一直是这样写的:

    ids = [i.replace('\n', '') for i in open(filename).readlines()]

    刚才意识到,原来readlines是多余

    ids = [i.replace('\n', '') for i in open(filename)]

    这样就好了

    2011-07-04 15:17:48 2人喜欢 4回应