VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之day17-python之文件操作

1.内置函数

复制代码
  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # print(abs(-1))
  4 # print(abs(1))
  5 #
  6 # print(all([1,2,'1']))
  7 # print(all([1,2,'1','']))
  8 # print(all(''))
  9 
 10 # print(any([0,'']))
 11 # print(any([0,'',1]))
 12 
 13 
 14 # print(bin(3))
 15 
 16 #空,None,0的布尔值为False,其余都为True
 17 # print(bool(''))
 18 # print(bool(None))
 19 # print(bool(0))
 20 
 21 name='你好'
 22 # print(bytes(name,encoding='utf-8'))
 23 # print(bytes(name,encoding='utf-8').decode('utf-8'))
 24 #
 25 # print(bytes(name,encoding='gbk'))
 26 # print(bytes(name,encoding='gbk').decode('gbk'))
 27 #
 28 # print(bytes(name,encoding='ascii'))#ascii不能编码中文
 29 
 30 # print(chr(46))
 31 
 32 # print(dir(dict))
 33 #
 34 # print(divmod(10,3))
 35 
 36 # dic={'name':'alex'}
 37 # dic_str=str(dic)
 38 # print(dic_str)
 39 
 40 #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型
 41 # print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf'))
 42 # print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23'))
 43 #
 44 # name='alex'
 45 # print(hash(name))
 46 # print(hash(name))
 47 #
 48 #
 49 # print('--->before',hash(name))
 50 # name='sb'
 51 # print('=-=>after',hash(name))
 52 
 53 
 54 # print(help(all))
 55 
 56 # print(bin(10))#10进制->2进制
 57 # print(hex(12))#10进制->16进制
 58 # print(oct(12))#10进制->8进制
 59 
 60 
 61 
 62 # print(isinstance(1,int))
 63 # print(isinstance('abc',str))
 64 # print(isinstance([],list))
 65 # print(isinstance({},dict))
 66 # print(isinstance({1,2},set))
 67 
 68 name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'
 69 # print(globals())
 70 # print(__file__)
 71 #
 72 # def test():
 73 #     age='1111111111111111111111111111111111111111111111111111111111111'
 74 #     print(globals())
 75 #     print(locals())
 76 #
 77 # test()
 78 
 79 # l=[1,3,100,-1,2]
 80 # print(max(l))
 81 # print(min(l))
 82 #
 83 #
 84 # print(list(zip(('a','n','c'),(1,2,3))))
 85 # print(list(zip(('a','n','c'),(1,2,3,4))))
 86 # print(list(zip(('a','n','c','d'),(1,2,3))))
 87 #
 88 p={'name':'alex','age':18,'gender':'none'}
 89 # print(list(zip(p.keys(),p.values())))
 90 # print(list(p.keys()))
 91 # print(list(p.values()))
 92 #
 93 # print(list(zip(['a','b'],'12345')))
 94 
 95 
 96 # l=[1,3,100,-1,2]
 97 # print(max(l))
 98 # print(min(l))
 99 
100 
101 age_dic={'alex_age':18,'wupei_age':20,'zsc_age':100,'lhf_age':30}
102 
103 # print(max(age_dic.values()))
104 #
105 # #默认比较的是字典的key
106 # print(max(age_dic))
107 #
108 # for item in zip(age_dic.values(),age_dic.keys()): #[(18,'alex_age')  (20,'wupeiqi_age') () () ()]
109 #     print(item)
110 #
111 # print('=======>',list(max(zip(age_dic.values(),age_dic.keys()))))
112 
113 # l=[
114 #     (5,'e'),
115 #     (1,'b'),
116 #     (3,'a'),
117 #     (4,'d'),
118 # ]
119 # l1=['a10','b12','c10',100] #不同类型之间不能进行比较
120 l1=['a10','a2','a10'] #不同类型之间不能进行比较
121 # print(list(max(l1)))
122 # print('--->',list(max(l1)))
123 
124 
125 
126 # l=[1,3,100,-1,2]
127 # print(max(l))
128 dic={'age1':18,'age2':10}
129 # print(max(dic)) #比较的是key
130 # print(max(dic.values())) #比较的是key,但是不知道是那个key对应的
131 
132 # print(max(zip(dic.values(),dic.keys()))) #结合zip使用
133 #
134 #
135 people=[
136     {'name':'alex','age':1000},
137     {'name':'wupei','age':10000},
138     {'name':'yuanhao','age':9000},
139     {'name':'linhaifeng','age':18},
140 ]
141 # max(people,key=lambda dic:dic['age'])
142 # print('周绍陈取出来没有',max(people,key=lambda dic:dic['age']))
143 
144 # ret=[]
145 # for item in people:
146 #     ret.append(item['age'])
147 # print(ret)
148 # max(ret)
149 
150 
151 # print(chr(97))
152 # print(ord('a'))
153 
154 # print(pow(3,3))  #3**3
155 # print(pow(3,3,2))  #3**3%2
156 
157 
158 
159 # l=[1,2,3,4]
160 # print(list(reversed(l)))
161 # print(l)
162 
163 #
164 # print(round(3.5))
165 
166 # print(set('hello'))
167 
168 # l='hello'
169 # s1=slice(3,5)
170 # s2=slice(1,4,2)
171 # print(l[3:5])
172 # print(l[s1])
173 # print(l[s2])
174 # print(s2.start)
175 # print(s2.stop)
176 # print(s2.step)
177 
178 
179 
180 l=[3,2,1,5,7]
181 l1=[3,2,'a',1,5,7]
182 # print(sorted(l))
183 # print(sorted(l1)) #排序本质就是在比较大小,不同类型之间不可以比较大小
184 # people=[
185 #     {'name':'alex','age':1000},
186 #     {'name':'wupei','age':10000},
187 #     {'name':'yuanhao','age':9000},
188 #     {'name':'linhaifeng','age':18},
189 # ]
190 # print(sorted(people,key=lambda dic:dic['age']))
191 name_dic={
192     'abyuanhao': 11900,
193     'alex':1200,
194     'wupei':300,
195 }
196 print(sorted(name_dic))
197 #
198 # print(sorted(name_dic,key=lambda key:name_dic[key]))
199 #
200 # print(sorted(zip(name_dic.values(),name_dic.keys())))
201 
202 # print(str('1'))
203 # print(type(str({'a':1})))
204 # dic_str=str({'a':1})
205 # print(type(eval(dic_str)))
206 
207 # l=[1,2,3,4]
208 # print(sum(l))
209 # print(sum(range(5)))
210 #
211 #
212 # print(type(1))
213 #
214 # msg='123'
215 #
216 # if type(msg) is str:
217 #     msg=int(msg)
218 #     res=msg+1
219 #     print(res)
220 
221 # def test():
222 #     msg='撒旦法阿萨德防撒旦浪费艾丝凡阿斯蒂芬'
223 #     print(locals())
224 #     print(vars())
225 # test()
226 # print(vars(int))
227 
228 #import------>sys----->__import__()
229 # import test
230 # test.say_hi()
231 
232 # import 'test'#报错
233 module_name='test'
234 m=__import__(module_name)
235 m.say_hi()
复制代码

 2.文件处理读操作

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # f=open('陈粒',encoding='utf-8')
 4 # data=f.read()
 5 # print(data)
 6 # f.close()
 7 #
 8 # f=open('xxx')
 9 # data=f.read()
10 # print(data)
11 
12 #r w a
13 f=open('陈粒','r',encoding='utf-8')
14 # data=f.read()
15 # print(data)
16 # print(f.readable())
17 # print('第1行',f.readline(),end='')
18 # print('第2行',f.readline())
19 # print('第3行',f.readline())
20 # # for i in range(1):
21 # #     pass
22 # print('第4行',f.readline())
23 # print('第5行',f.readline())
24 # print('第6行',f.readline())
25 # print('第7行',f.readline())
26 
27 data=f.readlines()
28 print(data)
29 f.close()
复制代码

3.文件处理写操作 不追加

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 f=open('陈粒','w',encoding='utf8')
 4 # f.read()
 5 f.write('11111111\n')
 6 f.write('222222222\n')
 7 f.write('333\n4444\n555\n')
 8 # f.writable()
 9 # f.writelines(['555\n','6666\n'])
10 # f.writelines(['555\n','6666\n',1]) # 文件内容只能是字符串,只能写字符串
11 f.close()
复制代码

4.文件处理写操作 追加

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 f=open('陈粒','a',encoding='utf-8')
4 f.write('写到文件最后')

5.文件处理其他模式

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # f=open('陈粒','r+',encoding='utf-8')
 4 # data=f.read()
 5 # print(data)
 6 # f.write('123sb')
 7 #
 8 #
 9 #
10 # f.write('sb')
11 
12 
13 #文件修改
14 # src_f=open('陈粒','r',encoding='utf-8')
15 # data=src_f.readlines()
16 # src_f.close()
17 # #
18 # for i in data:
19 #     print(i)
20 # print(data)
21 # dst_f=open('陈粒','w',encoding='utf-8')
22 # data=dst_f.readlines()
23 # dst_f.writelines(data)