* 之前有维护一套自己的CommonFunc.py(基础的、常用的小函数)
* 离职的时候发现拷不出来,很痛心>...<
* 重新写吧
* 碰到啥写啥
import io import os import numpy as np def read_file2list_2D(file_path, data_type=int, split_str=','): # 从文件读入二维数据,返回list out_lst = [] if not os.path.exists(file_path): print('Error: No such file', file_path) else: with open(file_path, 'r') as f: out_lst = np.loadtxt(io.StringIO(f.read().replace(split_str, ' '))) if len(out_lst): out_lst = [list(map(data_type, i)) for i in out_lst] else: print('Wrong: %s is empty' % file_path) return out_lst def mkdir_deep(file_path): # 深度创建文件夹,效果同 mkdir -p if not file_path: print('Erorr, %s is not a path' % file_path) return if os.path.exists(file_path): return os.makedirs(file_path) def comp_norm(Vector): # 根据矢量求标量 return math.sqrt(np.dot(Vector, Vector)) def comp_round(num, preci=0): # 四舍五入 # preci为0,返回int;preci不为0,返回float from decimal import Decimal, ROUND_HALF_UP if not isinstance(num, str): num = str(num) num = Decimal(num).quantize((Decimal('0.' + '0'*preci)), rounding=ROUND_HALF_UP) if preci == 0: num = round(int(num), preci) else: num = round(float(num), preci) return num
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)