python使用笔记006-函数+json操作

一、函数

1.1 函数的定义

 1 def hello():
 2     print('hello')
 3     print('fdsfjslkfs')
 4 
 5 #函数不调用就不会执行
 6 hello()
 7 
 8 def write_file(file_name,content):#形参,形式参数
 9     with open(file_name,'w',encoding='utf-8') as f:
10         f.write(content)
11 
12 
13 write_file('函数.txt','fdsfslfre分倒是累计额老人家')#实参
14 
15 #函数返回值
16 #函数没有写返回值时,默认返回None
17 #return:
18     #1、返回数据
19     #2、函数里只要遇到return 函数立马执行结束
20 def read_file(file_name):
21     with open(file_name,'r',encoding='utf-8') as f:
22         content = f.read()
23         return content
24 
25 content = read_file('函数.txt')
26 print(content)

1.2 函数的参数

 1 def a():#无参数
 2     pass
 3 
 4 def b(name,age):#必填参数,位置参数
 5     pass
 6 
 7 #默认值参数在调用函数时,如果传了参数值,则使用参数值,如果未传参数值,则用默认的参数值
 8 def op_file(file_name,content=None):#默认值参数
 9     with open(file_name,'a+',encoding='utf-8') as f:
10         if content:
11             f.write(str(content))
12         else:
13             f.seek(0)
14             result = f.read()
15             return result
16 
17 #可变参数
18 #1、这个是不是必须传的
19 #非必传
20 #2、限制不限制参数的个数
21 #不限个数
22 #3、args
23 #元组
24 def send_sms(*args):
25     print(type(args))
26     print(args)
27 
28 send_sms()
29 #send_sms('198','159')
30 send_sms('198','159','123','234','432','4342','098','198','159','123','234','432','4342','098')
31 
32 
33 #关键字参数
34 def test(**kwargs):#传入必须得指定关键字,kwargs是字典
35     print(kwargs)
36     print(type(kwargs))
37 
38 test(name='xiaolin',age=19,sex='')
39 
40 #如果四种参数一起用的话,顺序必须是:必填参数,默认值,可变参数,关键字参数
41 #必填参数必须写到默认值参数前面
42 #默认参数必须在可变参数前面
43 #可变参数必须在关键字参数
44 def test2(name,content=None,*args,**kwargs):
45     print(name)
46     print(content)
47     print(args)
48     print(kwargs)
49 
50 
51 def test3(name,phone,qq,addr,sex):
52     print(name)
53     print(phone)
54     print(qq)
55     print(addr)
56     print(sex)
57 
58 
59 test3('186','xiaolin','34334','beijing','')#根据定义的参数顺序来传参
60 test3(phone='123',name='fdsf',qq='re4343',sex='',addr='guangzhou')#指定参数名字来传参,跟参数位置无关
61 test3('cj001','185134',sex='',addr='guangzhou',qq='r90343')#组合传参
62 #test3('cj001','185134',sex='男',addr='guangzhou','r90343')#错误,不能这么写

1.3 函数小练习

 1 #判断传入参数是否为float类型
 2 #1、传参
 3 #2、把入参转成字符串
 4 #3、判断小数点个数是否为1
 5 #4、判断小数点前面的是否为整数
 6 #5、判断小数后面是否为整数
 7 def is_float(s):
 8     boolean = False
 9     s = str(s)
10     if s.count('.') == 1:
11         left,right = s.split('.')#根据小数点进行分割
12         if right.isdigit() and left.isdigit() and (int(left) != 0 or int(right) != 0):#左边和右边都是小数
13             boolean = True
14         if left.startswith('-') and left.count('-') == 1:
15             num = left[1:]
16             if num.isdigit() and right.isdigit() and (int(num) != 0 or int(right) != 0):
17                 boolean = True
18     return boolean
19 
20 
21 print(is_float(0.12))
22 print(is_float(1.12))
23 print(is_float(1))
24 print(is_float('sss.0001'))
25 print(is_float('sss.ssss'))
26 print(is_float(-1.23))
27 print(is_float('0.0'))
28 print(is_float('-0.0'))

执行结果如图所示:

 二、json操作

json就是一串字符串,如果在操作json时要获取值很难,将json转成字典就很好操作了

 1 import json
 2 #python的数据类型和json互相转换
 3 #json就是一串字符串
 4 d = {'key1':'v1','k2':'v2','k3':'v3','name':'刘婕'}
 5 json_str = json.dumps(d,indent=4,ensure_ascii=False)#字典转成字符串,转成json了
 6 print(json_str)
 7 
 8 #json.dump()将字典转成json,并写入文件中
 9 with open('u2.txt','w',encoding='utf-8') as f:
10     json.dump(d,f,indent=4,ensure_ascii=False)#indent 缩进,ensure_ascii=False 如果是中文就直接显示中文了
 1 import json
 2 json_str = '''
 3     {
 4     "key1": "v1",
 5     "k2": "v2",
 6     "k3": "v3",
 7     "name": "刘婕"
 8 }
 9 '''
10 dic = json.loads(json_str)#json字符串转字典
11 print(dic)
12 
13 #如果json不合法,就会报错
14 #比如json中有单引号
15 json_str1 = '''
16     {
17     'key1': "v1",
18     "k2": "v2",
19     "k3": "v3",
20     "name": "刘婕"
21 }
22 '''
23 dic1 = json.loads(json_str1)#会报错,报json格式不对
24 print(dic1)
25 
26 with open('users.txt') as f:
27     result = json.load(f)  #先读取文件内容,再将json转成字典
28     print(result)

报错如图所示:

 要查看json字符串格式是否正确,可以在下面网站进行查看

http://www.bejson.com/

猜你喜欢

转载自www.cnblogs.com/cjxxl1213/p/12814671.html