Python dataformat.py通用数据格式转化脚本


需求:在进行Hadoop测试时,需要造大量数据,例如某个表存在56列,但实际程序逻辑只适用到某几列,我们造的数据 也只需要某几列

构造几列数据,转化为对应数据表格式

涉及模块:os,getopt,sys

输入:源格式,文本文件

输出:目标格式,文本文件

 
  1. #!/usr/bin/python   
  2. # -*- coding: utf-8 -*-   
  3. #dataformat.py   
  4. #this script change data from your source to the dest data format   
  5. #2011-08-05 created version0.1   
  6. #2011-10-29 add row-row mapping ,default row value .rebuild all functions. version0.2    
  7. #next:add data auto generate by re expression   
  8.   
  9. import os,getopt,sys  
  10.   
  11. #读入文件,返回所有行   
  12. def read_file(path):  
  13.   f = open(path, "r")  
  14.   lines = f.readlines()  
  15.   f.close()  
  16.   return lines  
  17.   
  18. #处理一行,转为目标格式,返回目标行   
  19. def one_line_proc(parts, total, ft_map, outsp, empty_fill):  
  20.     toindex = 0  
  21.     outline = ""  
  22.     keys = ft_map.keys()  
  23.     for i in range(1, total+1):  
  24.       if i in keys:  
  25.         fill_index = ft_map[i]  
  26.         if fill_index.startswith("d"):  
  27.           outline += fill_index[1:]  
  28.         else:  
  29.           outline += parts[int(fill_index)-1]  
  30.       else:  
  31.         outline += empty_fill  
  32.       if i !=total:  
  33.         outline += outsp  
  34.       #TODO:加入使用默认值列  若是以d开头,后面是默认,否则取文件对应列 done   
  35.       #TODO:这里根据这个判断长度也需要换掉 done   
  36.     return outline  
  37.   
  38. #处理入口,读文件,循环处理每一行,写出   
  39. #输入数据分隔符默认\t,输出数据默认分隔符\t   
  40. def process(inpath, total, to, outpath, insp="\t", outsp="\t", empty_fill=""):  
  41.   #TODO:这里将to转为映射格式 done   
  42.   ft_map = {}  
  43.   in_count = 0  
  44.   used_row = []  
  45.   for to_row in to:  
  46.     if r"\:" not in to_row and len(to_row.split(":"))==2:  
  47.       used_row.append(int(to_row.split(":")[1]))  
  48.     if r"\=" not in str(to_row) and len(str(to_row).split("="))==2:  
  49.       pass  
  50.     else:  
  51.       in_count += 1  
  52.   
  53.   for to_row in to:  
  54.     if r"\=" not in str(to_row) and len(str(to_row).split("="))==2:  
  55.       ft_map.update({int(to_row.split("=")[0]):"d"+to_row.split("=")[1]})  
  56.       continue  
  57.     elif r"\:" not in to_row and len(to_row.split(":"))==2:  
  58.       ft_map.update({int(to_row.split(":")[0]):to_row.split(":")[1]})  
  59.       continue  
  60.     else:  
  61.       to_index = 0  
  62.       for i in range(1100):  
  63.         if i not in used_row:  
  64.           to_index = i  
  65.           break  
  66.       ft_map.update({int(to_row):str(to_index)})  
  67.       used_row.append(to_index)  
  68.   
  69.   lines = read_file(inpath)  
  70.   f = open(outpath,"w")  
  71.   result=[]  
  72.   for line in lines:  
  73.      parts = line.strip("\n").split(insp)  
  74.      #TODO:这里判断长度必须换掉 done   
  75.      if len(parts) >= in_count:  
  76.        outline = one_line_proc(parts, total, ft_map, outsp, empty_fill)  
  77.        result.append(outline+"\n")  
  78.   f.writelines(result)  
  79.   f.close()  
  80.   
  81. #打印帮助信息   
  82. def help_msg():  
  83.   print("功能:原数据文件转为目标数据格式")  
  84.   print("选项:")  
  85.   print("\t -i inputfilepath  [必输,原文件路径]")  
  86.   print("\t -t n              [必输,n为数字,目标数据总的域个数]")  
  87.   print("\t -a '1,3,4'        [必输,域编号字符串,逗号分隔。指定域用原数据字段填充,未指定用'0'填充]")  
  88.   print("\t -o outputfilepath [可选,默认为 inputfilepath.dist ]")  
  89.   print("\t -F 'FS'           [可选,原文件域分隔符,默认为\\t ]")  
  90.   print("\t -P 'OFS'          [可选,输出文件的域分隔符,默认为\\t ]")  
  91.   sys.exit(0)  
  92.   
  93. #程序入口,读入参数,执行   
  94. def main():  
  95.     try:  
  96.         opts,args = getopt.getopt(sys.argv[1:],"F:P:t:a:i:o:f:h")  
  97.   
  98.         for op,value in opts:  
  99.           if op in ("-h","-H","--help"):  
  100.             help_msg()  
  101.           if op == "-i":  
  102.             inpath = value  
  103.           elif op == "-o":  
  104.             outpath = value  
  105.           elif op == "-t":  
  106.             total = int(value)  
  107.           elif op == "-a":  
  108.             to = value.split(",")  
  109.           elif op == "-F":  
  110.             insp = value.decode("string_escape")  
  111.           elif op == "-P":  
  112.             outsp = value.decode("string_escape")  
  113.           elif op == "-f":  
  114.             empty_fill = value  
  115.         #考虑下这边放在神马地方合适   
  116.         if len(opts) < 3:  
  117.           print(sys.argv[0]+" : the amount of params must great equal than 3")  
  118.           sys.exit(1)  
  119.   
  120.     except getopt.GetoptError:  
  121.       print(sys.argv[0]+" : params are not defined well!")  
  122.       
  123.     if 'inpath' not in dir():  
  124.       print(sys.argv[0]+" : -i param is needed,input file path must define!")  
  125.       sys.exit(1)  
  126.       
  127.     if 'total' not in dir():   
  128.       print(sys.argv[0]+" : -t param is needed,the fields of result file must define!")  
  129.       sys.exit(1)  
  130.   
  131.     if 'to' not in dir():  
  132.       print(sys.argv[0]+" : -a param is needed,must assign the field to put !")  
  133.       sys.exit(1)  
  134.   
  135.     if not os.path.exists(inpath):  
  136.       print(sys.argv[0]+" file : %s is not exists"%inpath)  
  137.       sys.exit(1)  
  138.   
  139.     if 'empty_fill' not in dir():  
  140.       empty_fill = ''  
  141.   
  142.     tmp=[]  
  143.     for st in to:  
  144.       tmp.append(str(st))  
  145.     to=tmp  
  146.   
  147.     if 'outpath' not in dir():  
  148.       outpath = inpath+".dist"  
  149.   
  150.     if 'insp' in dir() and 'outsp' in dir():  
  151.       process(inpath,total,to,outpath,insp,outsp,empty_fill=empty_fill)  
  152.     elif 'insp' in dir():  
  153.       process(inpath,total,to,outpath,insp,empty_fill=empty_fill)  
  154.     elif 'outsp' in dir():  
  155.       process(inpath,total,to,outpath,outsp=outsp,empty_fill=empty_fill)  
  156.     else:  
  157.       process(inpath,total,to,outpath,empty_fill=empty_fill)  
  158.   
  159. if __name__ =="__main__":  
  160.     main()  

  • 1
  • 2
  • 下一页

相关内容