python生成ios固定代码模块脚本(仅供参考)

来源:互联网 发布:晨曦定额软件下载 编辑:程序博客网 时间:2024/06/05 07:38
#coding=utf8import csvimport osimport sysreload(sys)sys.setdefaultencoding("utf8")'''Author:ewangData:2017/07/18该模块的主要功能函数:读取csv中的数据到内存中readDataToList():把csv中的数据,数据项以字典类型存储在列表中。printIOSCodeData(dataList):参数类型是以字典类型作为单个数据项的列表'''PATH=lambda p:os.path.abspath(os.path.join(    os.path.dirname(__file__), p))class DataList(object):    def __init__(self,filePah=PATH("./20170803162912.csv")):        try:            #存放csv中读取的数据            self.mdbuffer=[]            #打开csv文件,设置读的权限            csvHand=open(filePah,"r")            #创建读取csv文件句柄            readcsv=csv.reader(csvHand)            #把csv的数据读取到mdbuffer中            for row in readcsv:                    self.mdbuffer.append(row)              #把数据穿件为为字典类型的        except Exception,e:            print "Read Excel  error:",e        finally:            #关闭csv文件            csvHand.close()                def readDataToList(self):        try:            #在数组最后添加一个空白行            #该行的作用是为了成功获取最后一条json数据            #在数组endLine添加空白字符            endLine=[" " for num in range(len(self.mdbuffer[1])) if num>=0]             #把以空字符的endLine添加到末尾            self.mdbuffer.append(endLine)            #获取mdbuffer中的元素个数            rowNumber=len(self.mdbuffer)            #设置当前行号            currentrow=1            #设置json数据的属性值            propertyJson={}            #读取列表中的元素               dataList=[]              try:                 for row in range(1,rowNumber):                    #创建一个临时变量用来存取一次循环的属性键值                    temp={}                    #获取列表中一个元素                    item=self.mdbuffer[row]                    #获取当前元素,当前元素代表的是每个                    #事件起始的位置                    currentItem=self.mdbuffer[currentrow]                    #获取serviceId并进行解码                    mdEvent=currentItem[0].decode("gbk")                    serviceId= currentItem[2].decode("gbk")                    #获取属性并进行解码,把解码的值存入propertyName                    propertyName=item[3].decode("gbk")                    #获取属性值并进行解码,把解码的值存入propertyValue                    propertyValue=item[4].decode("gbk")                    try:                        #判断埋点事件与serviceId是否相等                        if item[0]==currentItem[0] and item[2]==currentItem[2]:                            #用来保存埋点事件                            propertyJson["mdEvent"]=mdEvent                             #把serviceId方式字典propertyJson中                            propertyJson["serviceId"]=serviceId                             #把属性/值对放入temp字典中                                                                             temp[propertyName]=propertyValue                            #调用字典的update函数,把temp中的键值对                            #添加到 propertyJson字典中                            propertyJson.update(temp)                            #使用continue,如果为if条件为true则循环执行if语句模块                            continue                          else:                            #把行号设置为当前行                            currentrow=row                              #把当前的属性解码放入propertyName                                                propertyName=currentItem[3].decode("gbk")                            #把当前的属性值解码放入propertyName                            propertyValue=currentItem[4].decode("gbk")                            #用来保存埋点事件                            mdEvent=currentItem[0].decode("gbk")                            #把serviceId方式字典propertyJson中                             propertyJson["serviceId"]=serviceId                                #把属性/值对放入propertyJson字典中                              propertyJson[propertyName]=propertyValue                            #propertyJsonList.append(propertyJson)                             dataList.append(propertyJson)                            propertyJson={}                    except Exception,e:                        print "Get Property Json Error:" ,e                        #print "Get Property Json Error:",e            except Exception,e:                print "Get Date Error:",e                #print "Get Date Error:",e            #返回dataList            return  dataList             except Exception,e:            #把信息写入日志中            print "Reading Data TO Dic Error:",e                def printIOSCodeData(self,dataList):        try:            if len(dataList)>0:                #对列表中的数据执行for循环                #并输出类似与json样式的数据                for item in dataList:                        print u"埋点事件:",item["mdEvent"]                                                                      #输出键值对                        print "[XMBehaviorMgr postStaticWithAppName:@\"event\""                           print  "\t\t\t serviceId:@\"%s\"" %item["serviceId"]                        item.pop("serviceId")                        item.pop("mdEvent")                        print   "\t\t\t  otherProps:@{"                        for key,val in item.items():                                                       print "\t\t\t\t","@\""+key+"\"",":","@\""+val+"\"",","                        print "\t\t\t }"                        print " \t\t\t trackDic:nil];"                        #设置以#格式的分隔符                        print "#"*50                        print        except Exception,e:            print "OutPut IOS Data Error:" ,e              def test():    baseData=DataList()    baseData.printIOSCodeData(baseData.readDataToList())    if __name__=="__main__":    baseData=DataList(sys.argv[1])    baseData.printIOSCodeData(baseData.readDataToList())

原创粉丝点击