Python 1.3 元组与文件

时间:2022-10-30 20:17:33

一 Python元组Tuple类型

元组T= (1, 2, 3, 4)是不可变类型,属于序列,但顶层元素不可变,仅支持count()和index()操作。

 -*- coding:UTF- -*-

# 不可变类型
T = ( , 'Ni', , , )
T = T + tuple("spam")
for x in T:
print(x) T = [ x* for x in T ]
T = [ x for x in ['b', 'c', 'a', 'd']]
T = tuple(T)
tmp = list(T).sort() # sort()为列表对象排序操作,不返回值
print(tmp) # 打印None
# tuple 仅有两个的操作
if "aa" in T:
print( T.index("aa") )
print( T.count('bb') ) # 元组不可变性只支持顶层
T = ( , , [, ], )

二 文件类型

 F = open( filename, mode ) ,read(), readline(), readlines(),write(), close()

# 文件
myfile = open('myfile.txt', 'w')
myfile.write("python file text,\n")
myfile.write("end of text file.\n")
myfile.close()
# 迭代操作
myfile = open('myfile.txt', 'r')
S = myfile.read()
print(S)
myfile.close() for line in open('myfile.txt'):
print(line, end='')
myfile.close() # 文件存储和解析Python object
X, Y, Z = , ,
S = 'Spam'
D = {'a':, 'c':}
L = [ i for i in range()]
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)
"""
# convert str into python object
F = open("dataFile.txt")
line = F.readline()
print( line.rstrip() ) line = F.readline()
numbers = [ int(x) for x in line ]
print(numbers) # convert list and dict
line = F.readline()
parts = line.split("$") # eval(): convert str into object
print( [eval(P) for P in parts] )
F.close()

Pickle持久化存储Python原生对象

import pickle
F = open("dataFile.pkl", 'wb')
D = {'a': , 'e': , 'b': , 'c': }
pickle.dump( D, F) # 对象序列化
F.close()
F = open("dataFile.pkl", 'rb')
E = pickle.load(F)
print(E) # BOOL True Flase 数字0为Flase,其他都为真 空对象都为假
if bool() != bool([]):
print( bool('spam') )
if type() != type([]):
print( isinstance([], list) )
# 避免循环引用 L = ["refer"] L.append(L) # print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
a, b, c , *d= [, , , , , ]
print(a, b, c, sep=" | ", end ="!n" )
log = open("textFile.txt", "w")
print( a, b, c, d, sep="***", end="\n", file = log)
log.close()
# print函数默认将对象传入到stdout中,显示
import sys
sys.stdout.write("Hello\n")