示例#1
0
def migrate_file(path, data_type):
    if data_type=='data':
        records = []
        with open(path,'rb') as f:
            csvfile = csv.reader(f)
            next(csvfile)
            for line in csvfile:
                record = line[:]
                city_code = city_dict.get(line[0],'')
                record.insert(0,city_code)
                records.append(record)
        CityAQI.create_bulk(records)
    elif data_type=='log':
        with open(path,'rb') as f:
            for line in f:
                if 'ERROR' in line:
                    times,message = line.strip().split('ERROR :')
                    date,time = times.split(' ')[:2]
                    year, month, day = date.split('-')
                    hour, minute, second = time.split(':')
                    date_time = datetime(int(year), int(month), int(day),
                                        int(hour), int(minute), int(second), tzinfo=GMT8())
                    ErrorLog.create(message.strip(), date_time=date_time)
                    print('migrate one error log, log time is %s'%date_time)
示例#2
0
def persist(result):
    CityAQI.create(result)