开始学习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]