python 文件
2020-12-13 06:08
标签:调用 脚本 文件中 列表 pop bye one 文件读取 包括 #close是通常选项。调用close会终止外部文件的连接。 #写进文件 #读取文件 #goodbye text file print(open(‘myfile.txt‘).read()) #文件迭代器往往是最佳选择 #python2 #python3中的区别源自于简单文本和unicode文本并为一种常规的字符串 chars = open(‘datafile.txt‘).read() f = open(‘datafile.txt‘) line.rstrip() line = f.readline() parts = line.split(‘,‘) #用pickle存储python原生对象 #要生成一个打包的二进制数据文件,用wb模式打开它并将一个格式化字符串和几个python values = struct.unpack(‘>i4sh‘,data) #其他文件工具 #重访类型分类 #对象类型 分类 是否可变 #bytearray 序列 是 #引用vs拷贝 x = [1,2,3] import copy #比较,相等性,真值 s1 = ‘spam‘ d1 = {‘a‘:1,‘b‘:2} l = [None] *4 #内置的类型陷阱 l = [4,5,6] l = [‘grail‘] python 文件 标签:调用 脚本 文件中 列表 pop bye one 文件读取 包括 原文地址:https://blog.51cto.com/14418623/2419173从文件读取的数据回到脚本是一个字符串。
文件总是缓冲并且是可查的
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
#hello textfile
#goodbye text file
for line in open(‘myfile.txt‘):
print(line,end=‘‘)
#hello textfile
#goodbye text file
#python3文本文件内容为常规的字符串,自动执行Unicode编码和解码,默认行末换行。
二进制文件为一个特殊的bytes字符串
文本文件处理8位文本和二进制数据,有特殊的字符串类来处理unicodewenben
#因为所有的文本都是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()
print(chars)
#spam
#43,44,45
#[1, 2, 3]${‘a‘: 1, ‘b‘: 2}
line = f.readline()
print(line)
#spam
print(line)
#spam
print(line)
#43,44,45
print(parts)
#[‘43‘, ‘44‘, ‘45\n‘]
print(int(parts[1])) # 44
numbers = [int(p) for p in parts]
print(numbers) # [43, 44, 45]
#int和一些其他的转换方法会忽略旁边的空白
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}]
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.‘
#文件中打包二进制数据的存储于解析
#struct模块能够构造和解析打包的二进制数据
#对象传给struct,这里用的格式化字符串指一个4字节整数,一个包含4字符的字符串
#以及一个二位整数的数据包。这些都是按照高位在前的形式
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)
print(values) # (7, b‘spam‘, 8)
#标准流,sys模块中预先打开的文件对象如sys.stdout
#os模块中的描述文件
#socket。pipes。FIFO文件
#通过键开存储的文件
#shell流,op.popen和subprocess.Popen
#对象根据分类共享操作,如str,list,tuple都共享合并,长度,索引等序列操作
#只有可变对象可以原处修改
#文件导出的唯一方法
#对象分类数字 数值 否
字符串 序列 否
列表 序列 是
字典 映射 是
元组 序列 否
文件 拓展 N/A
sets 集合 是
frozenset 集合 否
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]
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}
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}
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
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 ......
d2 = {‘a‘:1,‘b‘:3}
print(sorted(d1.items()) print(sorted(d1.keys()) print(sorted(d1.values())
真值 ‘spam‘ 1
假值 ‘‘ [] {} () 0.0 None
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
#赋值生成引用,而不是拷贝
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‘]重复能增加层次深度
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.append(l)
print(l) # [‘grail‘, [...]]
#不可变类型不可再原处修改
t = (1,2,3)
t = t[:2] + (4,)
print(t) # (1, 2, 4)