Python学习笔记

开始学习Python了,写一个零零碎碎的笔记,

2017年4月23日23:38:40
pickle函数

import pickle
# 取
account_file = open('account_file.pkl', 'rb')
account = pickle.load(account_file)
account_file.close()
# 存
account_file = open('account_file.pkl', 'wb')
pickle.dump(account, account_file)
account_file.close()

2017年4月24日11:37:30
读取文件行数

file = open(_Path, 'r')
lines = len(file.readlines())
print(lines)

2017年4月24日15:05:24
读取文档但不能正常输出时:
修改file open

file1 = open(filename, 'r', encoding='UTF-8')

原因解决:文档的编码问题,设置为GBK的时候就不用特别添加文档要求了。也就正常了。

2017年4月25日17:31:09
readline()与readlines()的区别
readline() 每次读取一行,为str
readlines()读完整个文件,为list

2017年5月7日10:14:42
py2语法转py3语法差别
①、dict
dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).

Also, the dict.iterkeys(), dict.iteritems() and dict.itervalues() methods are no longer supported.

②、八进制
py2:0234 → py3:0o234
十进制到八进制语法上一样,oct(number),但是输出上有点儿差别,

③、try except as【相比较py2多一个as】

line = f.readline()
lines = readlines()
read 读取整个文件

readline 读取下一行

readlines 读取整个文件到一个迭代器以供我们遍历(读取到一个list中,以供使用,比较方便)、
http://www.cnblogs.com/hanggegege/p/5926549.html

list
a = [1, 2, 3]
b = a
修改a b元素会影响对方
a = [1, 2, 3]
b = list(a)
深复制

a = [1,2 ,3]
b = list(a)
c = 4*b

a[0] = 23

print(c)

a = [1,2 ,3]
b = [list(a)]
c = 4*b

a[0] = 23

print(c)

a = [1,2 ,3]
b = [a]
c = 4*b

a[0] = 23

print(c)

a = [1,2 ,3]
b = a
c = 4*b

a[0] = 23

print(c)
结果:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[23, 2, 3], [23, 2, 3], [23, 2, 3], [23, 2, 3]]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值