Python常用功能函数
来源:互联网 发布:eplan软件安装 编辑:程序博客网 时间:2024/05/29 04:50
1. 字符串反转
''' 字符串反转 @string:字符串变量 ''' @staticmethod def str_reverse(string): result = string[::-1] return result
2. 删除首尾指定的字符
''' 删除首尾指定的字符 @string:字符串变量 @rm:要删除的字符,默认为空格 ''' @staticmethod def str_strip(string,rm=''): if not rm.strip(): result = string.strip() else: result = string.strip(rm) return result
3. 生成uuid
''' 生成uuid ''' @staticmethod def get_uuid(): result = uuid.uuid1() return result
4. 获取文件内容
''' 获取文件内容 @fileName:文件名 ''' @staticmethod def getFileContent(fileName): result = "" if not os.path.exists(fileName): return result file = open(fileName) try: result = file.read() finally: file.close() return result
5. 文件写入
''' 文件写入 @fileName:文件名 @content:写入的文件内容 @append:是否追加,True追加,False覆盖 ''' @staticmethod def setFileContent(fileName,content,append=True): if not os.path.exists(fileName): return False if append: file = open(fileName,'a') else: file = open(fileName,'w') try: file.write(content) finally: file.close() return True
6. list排序
''' list排序 @listObj:list变量 ''' @staticmethod def list_sort(listObj): listObj.sort() return listObj
7. list反转
''' list反转 @listObj:list变量 ''' @staticmethod def list_reverse(listObj): listObj.reverse() return listObj
8. list去重
''' list去重 @listObj:list变量 ''' @staticmethod def list_unique(listObj): result = sorted(set(listObj),key=listObj.index) return result
9. dict按键排序
''' dict按key排序 @dictObj:数据字典变量 ''' @staticmethod def dict_sort_key(dictObj): result = sorted(dictObj.items(), key=lambda d: d[0]) return result
10. dict按值排序
''' dict按value排序 @dictObj:数据字典变量 ''' @staticmethod def dict_sort_value(dictObj): result = sorted(dictObj.items(), key=lambda d: d[1]) return result
11. dict键值对反转
''' 键值对反转 @dictObj:数据字典变量 ''' @staticmethod def dict_reverse(dictObj): result = dict(map(lambda t:(t[1],t[0]), dictObj.items())) return result
12. 日期格式化
''' 格式化日期 @timeStamp:时间戳,默认为当前时间 @format_str:目标格式,默认为%Y-%m-%d %H:%M:%S ''' @staticmethod def date_format(timeStamp='',format_str=''): if type(timeStamp) == type(1): timeStamp = str(timeStamp) if not timeStamp.strip(): dateArray = datetime.datetime.now() else: timeStamp = float(timeStamp) dateArray = datetime.datetime.utcfromtimestamp(timeStamp) if not format_str.strip(): format_str = '%Y-%m-%d %H:%M:%S' result = dateArray.strftime(format_str) return result
13. 字符串md5加密
''' 字符串加密(md5) @string:待加密的字符串 ''' @staticmethod def md5(string): result = hashlib.md5() result.update(str.encode(string)) return result.hexdigest()
14. json转换为dict
''' json转换为dict 注意:标准的json格式,键必须使用双引号引起来 @json_str:json字符串 ''' @staticmethod def json_to_dict(json_str): try: result = json.loads(json_str) except: result = json_str return result
15. dict转换为json
''' dict转换为json @dictObj:dict变量 ''' @staticmethod def dict_to_json(dictObj): result = json.dumps(dictObj) return result
友情提醒:
以上代码还需要相关模块的支持,如下:
os,uuid,datetime,hashlib,json
0 0
- Python常用功能函数
- Python 3.0 常用功能
- Python常用功能汇总
- python常用功能总结
- python常用常用函数
- python常用常用函数
- python常用常用函数
- python常用常用函数
- Javascript常用功能函数
- JavaScript常用功能函数
- js常用功能函数
- Oracle常用功能函数
- 常用功能函数
- Js常用功能函数
- python 功能函数
- python常用文本处理功能
- python 的一些常用功能
- python入门常用功能小结
- 八、多WebApps与多mysql的集成
- excel导入
- centOS安装pip
- Rxlifecycle使用详解
- HashSet(Collection<? extends E> c)
- Python常用功能函数
- DAY12:leetcode #26 Remove Duplicates from Sorted Array
- Java用BufferedImage和Graphics画图
- 单片机基本的外设通信接口
- linux下c++编程
- json、javaBean、xml互转的几种工具介绍
- Python自定义函数基础:参数预定义值
- oracle数据库tns配置方法详解
- hdu1394 Minimum Inversion Number --更新查询