您的位置:首页 > 编程语言 > Python开发

python入门之(元组、文件)

2015-09-27 10:30 661 查看
>>> #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 模块
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: