def write_xls(file,a): '''ValueError: row index was 65536, not allowed by .xls format''' import xlwt file=autoPath(file,ext='xls') xldoc = xlwt.Workbook() sheet = xldoc.add_sheet("Sheet1", cell_overwrite_ok=True) for i, row in py.enumerate(a): for j, col in py.enumerate(row): sheet.write(i, j, col) xldoc.save(file) return file
def dill_load(filename,return_value=False,set_user_ns=True): # F=py.importF() if filename.lower()[-5:] not in ['.dill','ickle']: for f in F.ls(U.gst,f=1,d=0): if filename in f: py.input('continue or ctrl+c using: '+f) filename=f break vars=[] dnv={} if U.all_in([',','='],filename): vars=T.regex_match_all(F.getNameWithoutExt(filename),T.RE_vars_separated_by_commas)[0].split(',') if vars: for n,v in py.enumerate(F.dill_load(filename,dill_ext='')): dnv[vars[n]]=v else: varname='' for c in F.getNameWithoutExt(filename): if c not in T.alphanumeric_:break varname+=c dnv[varname]=F.dill_load(filename,dill_ext='') if py.len(dnv)==1: r=[py.list(dnv)[0],filename] else: r=[py.list(dnv),filename] U.pln(r) if set_user_ns: for name,v in dnv.items(): gipy.user_ns[name]=v if return_value: return r
def write_xlsx(file,a): import openpyxl file=autoPath(file) if file.lower().endswith('.xls'):file=file[:-4]+'.xlsx' if not file.lower().endswith('.xlsx'):file=file+'.xlsx' outwb = openpyxl.Workbook() # 打开一个将写的文件 sheet = outwb.create_sheet(index=0) # 在将写的文件创建sheet for i, row in py.enumerate(a): for j, col in py.enumerate(row): sheet.cell(row=i+1, column=j+1).value=col # i,j=i+1,j+1 #我知道原因了,这是因为i+1 只应该每行执行一次,把它提到col循环外就行了 # try: # sheet.cell(row=i, column=j).value=col #这样出现错误表格格式 ,原因? # except Exception as e: # return e,sheet,i,j,col outwb.save(file) # 一定要记得保存 return file
def dill_dump(*vars,len=True): ''' pip install astor ''' T=py.importT() import ast co=sys._getframe().f_back.f_code dvars={} for index_a,e in enumerate(ast.walk(U.getAST(co) ) ): da={i:getattr(e,i) for i in dir(e) if not i.startswith('_') } # if index==1:break if ('value' in da) and py.isinstance( e.value,ast.Call): #ipy.*dump*() if U.getattr(e.value,'func','value','id')=='ipy' and \ ('dump' in U.getattr(e.value,'func','attr').lower() ): # print(index_a, da,'\n================\n') # U.set(e.value.args) for i,var in py.enumerate(e.value.args): # if py.isinstance( var,(ast.Name,ast.Subscript) ):# var : Name(lineno=1, col_offset=12, id='In', ctx=Load()), dvars[U.ast_to_code(var,EOL=False) ]=vars[i] if py.len(vars)==1 and not len: return F.dill_dump(obj=vars[0],file=py.list(dvars)[0]) r=[] for n,i in py.enumerate(dvars): il=U.len(dvars[i]) if il: if py.len(dvars)> 1:il='%-2s:%s'%(n,il) if py.len(dvars)==1:il='%-4s'%(il) else: il='%-2s'%n r.append( [ i, il ] ) if py.len(r)> 1:f='(%s) = {%s}' if py.len(r)==1: f='%s-%s' vars=vars[0] f=f % ( ','.join([i[0] for i in r]) ,','.join([i[1] for i in r])) f=f.replace('"""',"'") f=T.fileName(f) return F.dill_dump(obj=vars,file=f)
def try_dill_dump_recursively(obj,*a,): global U if not U:U=py.importU() if py.len(a)>TRY_MAX_LAYER:return try: b=dill_dump_bytes(obj) return (*a,U.size(b)) except Exception as e: if py.islist(obj) or py.istuple(obj) or py.isset(obj): r=[] for n,v in py.enumerate(obj): r.append([n,try_dill_dump_recursively(v,*a,n)]) return r elif py.isdict(obj): d={} for n,(k,v) in py.enumerate( obj.items()): d[n]=try_dill_dump_recursively(kv,*a,n) return d r=[] for n,k,v in U.dir(obj): r.append([k,try_dill_dump_recursively(v,*a,k)])
def recursive_test_dp(r): U=py.importU() if py.isdict(r) or py.getattr(r,'items',0): r=[[n,k,v] for n,(k,v) in py.enumerate(r.items()) ] if py.islist(r) or py.isdict(r): pass else:return r for n,k,v in r: try: b=dill_dump_bytes(v) r[n][2]=U.size(b) except Exception as e: r[n][2]=recursive_test_dp(v) return r
def outType(t=None,start=0,stop=U.IMAX,len=py.range(U.IMAX)): '''t is type to flit is3:range(start, stop[, step]) -> range object ''' if t !=None: # if type(t) is U.instance: # 这个什么意思来着 # t=t.__class__ if py.istr(t): def m(a):return t in py.repr(a) elif type(t) is U.classType:#没有考虑 取出Class 类型的情况 def m(a):return isinstance(a,t) else: if not isinstance(t,type): t=type(t) def m(a):return type(a) is t r={} for index,i in py.enumerate(gOut): if index<start or index>stop:continue if t !=None: if not m(gOut[i]):continue r[i]=type(gOut[i]),U.len(gOut[i]),U.sizeof(gOut[i]) return r
def read_xls_sheets_name(file): ''' XLRDError: Excel xlsx file; not supported''' import xlrd w=xlrd.open_workbook(file) return py.list(py.enumerate( w.sheet_names() ) )