小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Python
  5. 正文

Python chapter 10 learning notes

2017年10月18日 1711点热度 0人点赞 1条评论

·Reading data from file

oFormat:

with open('pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)

In line one, the function open(filename, 'x') is used to open a file, 'x' can be relpaced by 'w', 'r', 'a', 'r+'.

r只读,r+读写,不创建

w新建只写,w+新建读写,二者都会将文件内容清零

(以w方式打开,不能读出。w+可读写)

以a,a+的方式打开文件,附加方式打开

(a:附加写方式打开,不可读;a+: 附加读写方式打开)

以 'U' 标志打开文件, 所有的行分割符通过 Python 的输入方法(例#如 read*() ),返回时都会被替换为换行符\n. ('rU' 模式也支持 'rb' 选项) . 

r和U要求文件必须存在

不可读的打开方式:w和a

若不存在会创建新文件的打开方式:a,a+,w,w+

The key word with…as… is an easy way to name something as xxx.

Using the method read() can read the data to contents.

Using the method readlines() can read the data frome file, and then put them into a list.

Using the method strip() can delete the left blank.

·        Writing data into file

filename = 'programming.txt'
 
with open(filename, 'w') as file_object:
    file_object.write("I love python.")

NOTE: python can only writing string into text file. If you want to writing number into text  file, please use str() .

·        Exception

o   ZeroDivisionError

try:
    print(5/0)
except ZeroDivisionError:
    print("You can't divide by zero!")

Using try-except is good way to tell user that you can't divide by zero. It is also can be used in other exceptions.

·        If you don't want to tell user anything, you can just do as followed:

except ZeroDivisionError:
    pass

·        You can also use try-except-else, if it is not error, it can figure out the answer. For example,

print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")
while True:
    first = input("\nFirst Number: ")
    if first =='q':
        break
    second = input("Second number: ")
    try:
        answer = int(first)/int(second)
    except ZeroDivisionError:
        print("You can't divide by 0!")
    else:
        print(answer)

o   FileNotFoundError

Just like ZeroDivisionError

o   Analysis text file

If we meet this error:UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 1056: illegal multibyte sequence

We need to do this:

with open(filename, encoding='UTF-8') as something:#REMEMBER IT!

·        How to figure out the word number in one file?

Using the method split().For example,

words = contents.split()

·        HOW TO USE JSON?

o   json.dump() to storage data(存储数据)

o   json.load() to read data

For example,

import json
numbers = [2,3,4,5,6,7,8,9,10,11,12,13]
filename = 'numbers.json'
with open(filename, 'w') as f_obj:
    json.dump(numbers, f_obj)
with open(filename, 'r') as f_ob:
    numbs = json.load(f_ob)
print(numbs)

Reconsitution in the book.

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python python学习
最后更新:2017年10月18日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
沉痛哀悼在COVID-19疫情中逝去的战士和患者 莱芜市2011年普通高中招生工作实施意见2 2011年高考莱芜一中再创佳绩 [leetcode]题目解析(190702) C++必读书籍推荐 小花豆生活第7天:累并快乐着的第一周
标签聚合
python学习 学习 Python linux 生活 Java 鸟哥的linux私房菜 leetcode 高中 算法
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号