最近在写一个小程序,需要获取指定长度的字符串 number float time等类型的数据,所以自己封装了一个函数
#生成指定长度的字符串 def random_char(length,flag):#flag为0表示number,1表示字符串类型,2表示time类型 result = '' chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRRSSTtUuVvWwXxYyZz0123456789' nums = '123456789' numlength = len(nums) - 1 charlength = len(chars) - 1 str_tmp = Random() if flag == 1: result = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return result elif flag == 2: result = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") return result elif flag == 3 or flag == 4: for i in range(int(length)): if flag == 3: #1表示date,2表示timestamp,3表示number,4表示varchar2 nvarchar2 long char,5表示float result += nums[str_tmp.randint(0,numlength)] elif flag == 4: result += chars[str_tmp.randint(0,charlength)] elif flag == 5:#length == P20S8 即PRECISION = 20 SCALE = 8 precision,scale = get_precision_scale(length) integer_max = int(precision) - int(scale) for j in range(integer_max): result += nums[str_tmp.randint(0,numlength)] result += '.' for k in range(int(scale)): result += nums[str_tmp.randint(0,numlength)] return result原文链接:https://www.f2er.com/oracle/211141.html