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 模块
>>>
>>> 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 模块
相关文章推荐
- python(四)字典,集合,可变与不可变对象(下)
- python map
- Python namedtuple
- Python语法基础_控制语句_输入输出语句详解
- python tab
- python中执行命令行的方法
- python3.X 与其他语言小不同
- python-生成器
- python处理文件
- 生日悖论的Python实现
- Python游戏引擎开发(二):创建窗口以及重绘界面
- 20150926 kaggle Titanic入门篇excel&python.md
- 【Python】使用 multiprocessing.dummy 执行多线程任务
- python - bing_api_search
- 我的python学习笔记(2) - GDAL与Python
- Python 安装boto boto3
- mysql-python 安装错误: Cannot open include file: 'config-win.h': No such file or directory
- 学习python处理python编码问题
- python编码
- 使用 Python 连接 Caché 数据库