poython-16文件

day15回顾

  • 生成器

    • 生成器函数
      含有yield 语句的函数
      1. 返回生成器对象(生成器对象也是可迭代对象)
      2. 执行顺序与普通函数不同
        1. 当next(it)函数调用时,生成器函数才执行
        2. 当遇到yield语句时,保存当前函数的执行状态,将yield 表达式 结果返回给next(it) 函数
        3. 生成器的返回会触发StopIteration异常
          def myyield():
          yield 1
          yield 2
    • 生成器表达式:
      (表达式 for 变量 in 可迭代对象 if …)
  • 迭代工具函数
    zip(*iterables)
    zip([1,2,3], “ABCDE”)
    (1, ‘A’)
    (2, ‘B’)
    (3, ‘C’)
    enumerate(iterable, start=0)
    enumerate(“ABC”)
    (0, ‘A’)
    (1, ‘B’)
    (2, ‘C’)
    enumerate(“ABC”, 100)
    (100, ‘A’)
    (101, ‘B’)
    (102, ‘C’)

  • 序列
    字节串 bytes
    不可变序列
    字面值:
    b’’ b"" b’’’’’’ b""""""
    b’hello’

    • 字节数组 bytearray
      可变的序列

    • 构造函数:
      bytes() / bytearray()
      bytes(整数可迭代对象)/bytearray(整数…)
      bytes(整数n) / bytearray(整数n)
      bytes(字符串,encoding=‘utf-8’) /…
      运算:
      + += * *=
      < <= > >= == !=
      in / not in
      索引和切片

    • 字节
      0(0b00000000) ~ 255(0b11111111)

day16笔记:

文件 File

什么是文件
文件是用于数据的存储的单位
文件通常用于长期存储数据
文件中的数据是以字节为单位进行顺序存储的

文件的操作流程:

  1. 打开文件
  2. 读/写文件
  3. 关闭文件
  • 文件的打开函数:
    open(file, mode=‘rt’) 用于打开一个文件,返回此文件流对象,如果打开失败,则会触发OSError错误!
    file 是文件路径名的字符串
    mode 是打开的模式

  • 文件的关闭方法:
    F.close() # 关闭已经打开的文件,释放系统资源

  • 文本文件的基本操作:

    • 读文件:
      F.readline() # 读取一行文字
      F.readlines() # 读取多行文字
      F.read(n) # 读取n个字符
      写文件:
      F.write(字符串) # 写字符串到一个已经打开的文件
      F.writelines(字符串列表) # 写多个字符串到已经打开文件中
  • 练习:
    将如下数据用编辑器写入到文件data.txt中,数据如下:
    小李 13888888888
    小赵 13999999999
    张三丰 010-88888888
    写程序读取文件中的数据,打印出姓名和电话号码,格式如下:
    姓名: 小李 电话: 13888888888
    姓名: 小赵 电话: 13999999999
    姓名: 张三丰 电话: 010-88888888

  • 文本文件读写时各操作系统的换行符问题
    Linux换行符: ‘\n’ # LF
    Window换行符: ‘\r\n’ # CRLF
    旧的Macintosh的换行符: ‘\r’
    新的Mac OS X 的换行符: ‘\n’

  • 文本文件操作说明:
    文本模式下,各操作系统的换行符在读入python内部时会自动转换为’\n’

  • 写文本文件的示例见:
    file_write.py
    打开模式:
    ‘w’
    ‘x’
    ‘a’

  • 练习:
    1.写一个程序,输入很多人的姓名,年龄,家庭住址,保存在文件"infos.txt"中
    (格式自定义,建议用空格或逗号分隔符)
    如: info.txt
    小李 20 北京市朝阳区
    小张 18 上海市浦东新区
    2. 写一个程序,读入infos.txt中的内容,以如下格式打印:
    姓名: 小张, 年龄:20, 住址: 北京市朝阳区

  • 文件的迭代读取

    • 文件流对象是可迭代对象,迭代过程将以换行符’\n’作为分隔符依次获取
      如:
      f = open(“infos.txt”)
      for line in f:
      print(line)
  • 二进制文件操作
    操作模式字符: ‘b’
    默认文件中存储的都是以字节(bytes为单位的数据,通常有人为规定的格式)
    对二进制文件的读写需要用字节串(bytes)进行操作

  • F.read()的返回类型:
    对于文本模式. F.read()返回类型为字符串str
    对于二进制模式, F.read()返回字节串bytes

  • F.tell() # 返回当前文件流的绝对位置
    示例见: tell.py

  • F.seek(偏移量, whence=相对位置))

    • 偏移量:
      大于0的数代表相后偏移(文件尾方向)
      小于0的数代表向文件头方向偏移
    • 相对位置:
      0 代表文件头位置开始偏移
      1 代表从当前位置开始偏移
      2 代表从文件尾开始偏移
  • F.flush() 清空缓冲区

  • 标准输入输出文件:
    sys.stdin 标准输入文件(默认为键盘)
    sys.stdout 标准输出文件(默认为屏幕终端)
    sys.stderr 标准错误输入文件(默认为屏幕终端)

  • 汉字编码问题
    问题:
    十个汉字占多少个字节?
    20个? 30个?

  • 汉字编码:

    • 国标系列:
      GB18030(二字节或四字节编码,共27533个汉字)
      GBK(二字节编码,共21003个汉字)
      GB2312(二字节编码,共6763个汉字)
      注: Windows常用
      国际标准:
      Unicode <—> UTF-8
      Unicode16
      Unicode32
      (Linux / Mac OS X / Android 常用)
  • python 的编码字符串:
    ‘gb18030’
    ‘gbk’
    ‘gb2312’
    ‘utf-8’
    ‘ascii’

  • 编码注释:
    在源文件的第一行或第二行写入如下内容为编码注释
    # -- coding:gbk --

    # -- coding:utf-8 --
    作用:
    告诉python解释执行器,当前文件的编码是什么?

  • 练习:

    1. 写程序实现复制文件功能:
      要求:
      1. 源文件路径和目标文件路径需手动输入
      2. 要考虑关闭文件问题
      3. 要考虑复制超大文件问题
      4. 要能复制二进制文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Use Python’s built-in features to create innovative graphics for data visualization and technical illustrations. This book goes beyond simple commands and libraries to explain how to not only display but also rotate, shade, and edit graphics for any purpose. Starting with the essential Python functions to set up a plotting space and produce 2 and 3-dimensional objects, you’ll learn how to construct more complex objects, translate and rotate them, remove hidden lines, introduce shading to add realism, and project images to visualize any dataset. The final chapter includes several worked applications in science and engineering including planetary models, which you can adapt for your own use. Written for developers who want to harness Python’s capabilities to fine-tune their images, Python Graphics covers the different commands for plotting dots, lines, arrows, or arcs, creating custom plotting grids, correcting distortions, adding text and labels to illustrations, manipulating arcs and circles, specify and use colors, and more. Armed with these techniques and core math skills, you’ll be ready to create and customize detailed technical illustrations or data visualizations. What You’ll Learn Use Python′s built-in commands for plotting, removing distortions, and rotating objects Create and edit 2D and 3D objects Develop illustrations for scientific and engineering applications Who This Book Is For Python developers looking for tips on how to create illustrations and visualizations, as well as scientists, engineers, or students using Python. It assumes familiarity with vectors, matrices, geometry and trigonometry.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值