您好,登錄后才能下訂單哦!
#close是通常選項。調(diào)用close會終止外部文件的連接。
#寫進文件
myfile = open('myfile.txt', 'w')
myfile.write('hello textfile\n')
myfile.write('goodbye text file\n')
myfile.close()
#讀取文件
myfile = open('myfile.txt')
print(myfile.readline())
print(myfile.readline())
print(myfile.readline())
#hello textfile
#goodbye text file
print(open('myfile.txt').read())
#hello textfile
#goodbye text file
#文件迭代器往往是最佳選擇
for line in open('myfile.txt'):
print(line,end='')
#hello textfile
#goodbye text file
#python3
#python2
#python3中的區(qū)別源自于簡單文本和unicode文本并為一種常規(guī)的字符串
#因為所有的文本都是unicode,包括ascii和其他8位編碼
#文件中處理解析python對象
x, y, z = 43, 44, 45
s = 'spam'
d = {'a':1, 'b':2}
l = [1, 2, 3]
f = open('datafile.txt','w')
f.write(s +'\n')
f.write('%s,%s,%s\n' % (x, y, z))
f.write(str(l) +'$' +str(d) + '\n')
f.close()
chars = open('datafile.txt').read()
print(chars)
#spam
#43,44,45
#[1, 2, 3]${'a': 1, 'b': 2}
f = open('datafile.txt')
line = f.readline()
print(line)
#spam
line.rstrip()
print(line)
#spam
line = f.readline()
print(line)
#43,44,45
parts = line.split(',')
print(parts)
#['43', '44', '45\n']
print(int(parts[1])) # 44
numbers = [int(p) for p in parts]
print(numbers) # [43, 44, 45]
#int和一些其他的轉(zhuǎn)換方法會忽略旁邊的空白
line = f.readline()
print(line) # [1, 2, 3]${'a': 1, 'b': 2}
parts = line.split('$')
print(parts) # ['[1, 2, 3]', "{'a': 1, 'b': 2}\n"]
print(eval(parts[0])) # [1, 2, 3]
obj = [eval(p) for p in parts]
print(obj) # [[1, 2, 3], {'a': 1, 'b': 2}]
#用pickle存儲python原生對象
d = {'a':1, 'b':2}
f = open('datafile.pkl','wb')
import pickle
pickle.dump(d,f)
f.close()
f = open('datafile.pkl','rb')
e = pickle.load(f)
print(e) # {'a': 1, 'b': 2}
print(open('datafile.pkl','rb').read())
#b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02u.'
#文件中打包二進制數(shù)據(jù)的存儲于解析
#struct模塊能夠構(gòu)造和解析打包的二進制數(shù)據(jù)
#要生成一個打包的二進制數(shù)據(jù)文件,用wb模式打開它并將一個格式化字符串和幾個python
#對象傳給struct,這里用的格式化字符串指一個4字節(jié)整數(shù),一個包含4字符的字符串
#以及一個二位整數(shù)的數(shù)據(jù)包。這些都是按照高位在前的形式
f = open('data.bin','wb')
import struct
data = struct.pack('>i4sh',7,b'spam',8)
print(data)
f.write(data)
f.close()
#f = open('data.bin', 'rb')
#data = f.read()
#print(data)
values = struct.unpack('>i4sh',data)
print(values) # (7, b'spam', 8)
#其他文件工具
#標準流,sys模塊中預先打開的文件對象如sys.stdout
#os模塊中的描述文件
#socket。pipes。FIFO文件
#通過鍵開存儲的文件
#shell流,op.popen和subprocess.Popen
#重訪類型分類
#對象根據(jù)分類共享操作,如str,list,tuple都共享合并,長度,索引等序列操作
#只有可變對象可以原處修改
#文件導出的唯一方法
#對象分類
#對象類型 分類 是否可變
#bytearray 序列 是
l = ['abc', [(1,2),([3],4)],5]
print(l[1]) # [(1, 2), ([3], 4)]
print(l[1][1]) # ([3], 4)
print(l[1][1][0]) # [3]
#引用vs拷貝
x = [1,2,3]
l = ['a',x,'b']
print(l) # ['a', [1, 2, 3], 'b']
d = {'x':x,'y':2}
print(d) # {'x': [1, 2, 3], 'y': 2}
x[1] = 'surprise'
print(l) # ['a', [1, 'surprise', 3], 'b']
print(d) # {'x': [1, 'surprise', 3], 'y': 2}
x = [1,2,3]
l = ['a',x[:],'b']
print(l) # ['a', [1, 2, 3], 'b']
d = {'x':x[:],'y':2}
print(d) # {'x': [1, 2, 3], 'y': 2}
x[1] = 'surprise'
print(l) # ['a', [1, 2, 3], 'b']
print(d) # {'x': [1, 2, 3], 'y': 2}
import copy
l = [1,2,3]
d = {'a':1,'b':2}
e = l[:]
D = d.copy()
#比較,相等性,真值
l1 = [1,2,4]
l2 = [1,2,4]
print(l1 == l2, l1 is l2) # True False
s1 = 'spam'
s2 = 'spam'
print(s1 == s2, s1 is s2) # True True
a = 'a long strings qqq'
b = 'a long strings qqq'
print(a == b, a is b) # True True ......
d1 = {'a':1,'b':2}
d2 = {'a':1,'b':3}
print(sorted(d1.items()) < sorted(d2.items())) # True
print(sorted(d1.keys()) < sorted(d2.keys())) # False
print(sorted(d1.values()) < sorted(d2.values())) # True
l = [None] *4
print(l) # [None, None, None, None]
print(type([1]) == type([])) # True
print(type([1]) == list) # True
print(isinstance([1],list)) # True
import types
def f():pass
print(type(f) == types.FunctionType) # True
#內(nèi)置的類型陷阱
#賦值生成引用,而不是拷貝
l = [1,2,3]
m = ['x',l,'y']
print(m) # ['x', [1, 2, 3], 'y']
l[1] = 0
print(m) # ['x', [1, 0, 3], 'y']
#為了避免這種問題,可以用分片來生成一個高級拷貝
l = [1,2,3]
m = ['x',l[:],'y']
l[1] = 0
print(m) # ['x', [1, 2, 3], 'y']
l = [4,5,6]
x = l 3
y = [l] 3
print(x) # [4, 5, 6, 4, 5, 6, 4, 5, 6]
print(y) # [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
l[1] = 0
print(x) # [4, 5, 6, 4, 5, 6, 4, 5, 6]
print(y) # [[4, 0, 6], [4, 0, 6], [4, 0, 6]]
l = ['grail']
l.append(l)
print(l) # ['grail', [...]]
#不可變類型不可再原處修改
t = (1,2,3)
t = t[:2] + (4,)
print(t) # (1, 2, 4)
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。