使用Python进行数据类型转换(下)

我们发现代码比较长,很多地方都重复写了,这时候需要考虑写函数。
我们处理了3类数据:数字型(整数和小数)、日期型、布尔型。
数字型:

def parse_float(f):
     if f == '':
        return None
     else:
        return int(float(f))

整数型:

def parse_maybe_int(i):  
     if i == '':  
        return None
     else:
        return int(i)

日期型:

from datetime import datetime 
 def parse_date(date):
     if date == '':
        return None
     else:
        return datetime.strptime(date, '%Y-%m-%d')

布尔型:

def compare_bool(b):  
     if b ==  'True':  
        return True
     else:
        return False

对enrollments数据进行转换

for enrollment in enrollments:
     enrollment['cancel_date'] = parse_date(enrollment['cancel_date'])
     enrollment['days_to_cancel'] = parse_maybe_int(enrollment['days_to_cancel']) 
     enrollment['is_canceled'] = compare_bool( enrollment['is_canceled'])
     enrollment['is_udacity'] = compare_bool( enrollment['is_udacity'])
     enrollment['join_date'] = parse_date(enrollment['join_date'])

 enrollments[0]

对engagement数据进行转换

for engagement_record in daily_engagement:
     engagement_record['lessons_completed'] = parse_float((engagement_record['lessons_completed']))
     engagement_record['num_courses_visited'] = parse_float(float(engagement_record['num_courses_visited']))
     engagement_record['projects_completed'] = parse_float(engagement_record['projects_completed']))
     engagement_record['total_minutes_visited'] = float(engagement_record['total_minutes_visited'])
     engagement_record['utc_date'] = parse_date(engagement_record['utc_date'])

 daily_engagement[0]

剩下的submission也是同样处理方式。

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注