def handle(self, *args, **options): # get site name self.site = options["site"].strip().upper() self.s = Session() self.location = int(options["location"]) # get data waits = [] with open(options["file"]) as fd: print "start..." file_mod = None batch_counter = 0 for row in fd: if not row.startswith("0,08,"): continue try: # 批量提交 if batch_counter == 10000: print "finish 10000 row" self.s.commit() batch_counter = 0 batch_counter += 1 print batch_counter row = row.strip(" \r\n").decode("gb2312").split(",") row = [x.strip('"') for x in row] # 商品信息 if int(row[6]) == 1 and int(row[7]) in [2, 14]: ins = self.insert_record(row) if ins is not None: waits.append(ins) # 结算信息 elif int(row[6]) == 0 and int(row[7]) == 7: if row[8] == u"现金": payment_type = PaymentType.CASH elif row[8] == u"壳牌车队卡": payment_type = PaymentType.VIP elif row[8] == u"信用卡": payment_type = PaymentType.CREDIT else: payment_type = PaymentType.CASH # 将等待的doc赋值提交 for wait in waits: wait.payment_type = payment_type self.s.add(wait) waits = [] except Exception, e: print e self.s.commit() self.s.close()
class Command(BaseCommand): help = 'Import Trans from CSV' option_list = BaseCommand.option_list + ( make_option('--site',help="set site name",type="string"), make_option('--file',help="set file path",type="string"), make_option('--location',help="set location",type="string"), ) def handle(self, *args, **options): #get site name self.site=options['site'].strip().upper() self.s = Session() self.location=int(options['location']) with open(options['file']) as fd: print 'start...' file_mod=None batch_counter=0 for row in fd: #取得数据 row=row.replace('\t',' ') row=re.findall('\S+',row) #判断是否有效数据 if len(row)!=21: continue try: #批量提交 if batch_counter==10000: print 'finish 10000 row' self.s.commit() batch_counter=0 batch_counter+=1 print batch_counter ins=self.insert_record(row) if ins is None: continue self.s.add(ins) except Exception,e: print e self.s.commit() self.s.close() print 'end...'
def handle(self, *args, **options): #get site name self.site=options['site'].strip().upper() self.s = Session() self.location=int(options['location']) with open(options['file']) as fd: print 'start...' file_mod=None batch_counter=0 for row in fd: #取得数据 row=row.replace('\t',' ') row=re.findall('\S+',row) #判断是否有效数据 if len(row)!=21: continue try: #批量提交 if batch_counter==10000: print 'finish 10000 row' self.s.commit() batch_counter=0 batch_counter+=1 print batch_counter ins=self.insert_record(row) if ins is None: continue self.s.add(ins) except Exception,e: print e self.s.commit() self.s.close()