python入门之(元组、文件)

本文详细介绍了元组的基本操作、不可变性及其在文件输入输出中的应用,包括元组的数学运算、序列化、排序、元素查找、计数等特性,并展示了如何通过元组进行简单的文件读写操作。
>>> #tuple   file
>>> 
>>> dir(tuple)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> 
>>> # 元组中没有方法
>>> 
>>> (1, 2)+(3, 4)
(1, 2, 3, 4)
>>> (1, 2) * 4
(1, 2, 1, 2, 1, 2, 1, 2)
>>> T = (1, 2, 3, 4)
>>> T[0], T[1 : 3]
(1, (2, 3))
>>> 
>>> 
>>> T =("cc", "aa", "dd", "bb")
>>> T
('cc', 'aa', 'dd', 'bb')
>>> temp = list(T)
>>> temp.sort()
>>> temp
['aa', 'bb', 'cc', 'dd']
>>> T = tuple(temp)
>>> T
('aa', 'bb', 'cc', 'dd')
>>> 
>>> T =("cc", "aa", "dd", "bb")
>>> sorted(T)
['aa', 'bb', 'cc', 'dd']
>>> 
>>> T = (1, 2, 3, 2, 4, 2)
>>> T.index(2)
1
>>> T.index(2,2)
3
>>> T.count(2)
3
>>> 
>>> # 元组的不可变性只适用于元组本身顶层而并非其内容
>>> T = (1, [2, 3], 4)
>>> T[1] = 'meto'
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    T[1] = 'meto'
TypeError: 'tuple' object does not support item assignment
>>> T[1][0] = 'meto'
>>> T
(1, ['meto', 3], 4)
>>> 
>>> 
>>> import os
>>> os.getcwd()
'C:\\Windows\\system32'
>>> os.chdir("f:/work_python")
>>> os.getcwd()
'f:\\work_python'
>>> myfile = open('myfile.txt','w')
>>> myfile.write('hello text file\n')
16
>>> myfile.close()
>>> 
>>> 
>>> myfile = open('myfile.txt')
>>> myfile.readline()
'hello text file\n'
>>> myfile.readline()
''
>>> 
>>> open('myfile.txt').read()
'hello text file\n'
>>> print(open('myfile.txt').read())
hello text file


>>> myfile.close()
>>> myfile = open('myfile.txt', 'w')
>>> myfile.write('hello text file\n  meto')
22
>>> myfile.close()
>>> 
>>> for line in open('myfile'):
print(line)



Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    for line in open('myfile'):
FileNotFoundError: [Errno 2] No such file or directory: 'myfile'
>>> for line in open('myfile.txt'):
print(line)



hello text file


  meto
>>> myfile.close()
>>> 
>>> 
>>> x , y , z = 43 , 44 ,  45
>>> s = 'meto'
>>> d = {'a':1, 'b':2, 'c':3}
>>> l = [1, 2,3 ]
>>> f = open('datafile.txt', 'w')
>>> f.write(s + '\n')
5
>>> f.write('%s,%s,%s\n' %(x , y , z))
9
>>> f.write(str(l)+'$'+str(d)+'\n')
35
>>> f.close()
>>> 
>>> 
>>> chars = open('datafile.txt').read()
>>> chars
"meto\n43,44,45\n[1, 2, 3]${'a': 1, 'c': 3, 'b': 2}\n"
>>> print(chars)
meto
43,44,45
[1, 2, 3]${'a': 1, 'c': 3, 'b': 2}


>>> f = open('datafile.txt')
>>> line = f.readline()
>>> line
'meto\n'
>>> line.rstrip()
'meto'
>>> line = f.readline()
>>> line
'43,44,45\n'
>>> print(line)
43,44,45


>>> line = f.readline()
>>> line
"[1, 2, 3]${'a': 1, 'c': 3, 'b': 2}\n"
>>> parts = line.split('$')
>>> parts
['[1, 2, 3]', "{'a': 1, 'c': 3, 'b': 2}\n"]
>>> eval(parts[0])
[1, 2, 3]
>>> objects = [eval(p) for p in parts]
>>> objects
[[1, 2, 3], {'a': 1, 'c': 3, 'b': 2}]
>>> 
>>> #pickle模块
>>> 
>>> #struct 模块
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值