python中一个函数实现读写文件、判断价格正确常用函数

# 写一个函数,有2个功能,能读文件,也能写文件

# 读文件,只需要传文件名

# 写文件,需要传文件名,写入的内容

import json


def op_file(file, content=None):
if content:
with open(file, 'w', encoding='utf-8') as fw:
json.dump(content, fw)
else:
with open(file, encoding='utf-8') as fw:
result = json.load(fw)
return result

# 2、判断一个数为float或者正数 价格
def check_float(num):
num = str(num)
if num.isdigit() and int(right) > 0:
return True
elif num.count('.') == 1:
left, right = num.split('.')
if left.isdigit() and right.isdigit() and int(right) > 0:
return True
return False

猜你喜欢

转载自www.cnblogs.com/skyxiuli/p/10740391.html
今日推荐