def gxp_file_reader(fn):
rdr = csv.DictReader(open(fn, "r"), delimiter=str("\t"))
for rec in rdr:
if rec["id"].startswith("#"):
continue
yield rec
python类DictReader()的实例源码
def load_new_casecops(apps,schema_editor):
infile_path = BASE_DIR + '/data/20160614_migration/casecops.csv'
infile = open(infile_path)
incsv = csv.DictReader(infile)
for row in incsv:
case_lookup = list(Case.objects.filter(case_no=row['case_no']))
if len(case_lookup) != 1:
print 'ambiguous case:', row['case_no'], 'has len:', len(case_lookup)
import ipdb; ipdb.set_trace()
else:
# can only create casecop if we verify there's 1 matching case in cases table
case = case_lookup[0]
cc = CaseCop.objects.create(
id = row['id'],
case = case,
case_no = row['case_no'],
slug = '',
cop = Cop.objects.get(id=row['cop_id']) if row['cop_id'] else None,
cop_first_name = row['cop_first_name'],
cop_middle_initial = row['cop_middle_initial'],
cop_last_name = row['cop_last_name'],
badge_no = row['badge_no'],
officer_atty = row['officer_atty'],
officer_atty_firm = row['officer_atty_firm'],
entered_by = row['entered_by'],
entered_when = parse_str_date(row['entered_when']),
fact_checked_by = row['fact_checked_by'],
fact_checked_when = parse_str_date(row['fact_checked_when']),
matched_by = row['matched_by'],
matched_when = parse_str_date(row['matched_when']),
note = row['note'],
flag = row['flag'] == '1'
)
cc.save()
def load_new_cases(apps,schema_editor):
in_file_path = BASE_DIR + '/data/20160620_migration/cases.csv'
in_file = open(in_file_path)
in_csv = csv.DictReader(in_file)
for row in in_csv:
c = Case.objects.create(
id = row['id'],
case_no = row['CaseNumber'],
date_filed = parse_str_date(row['DateFiled']),
date_closed = parse_str_date(row['DateClosed']),
judge = row['Judge'],
plaintiff_atty = row['PlaintiffsLeadAttorney'],
plaintiff_firm = row['PlaintiffsAttorneyLawFirm'],
city_atty = row['CitysLeadAttorney'],
city_firm = row['CitysAttorneyLawFirm'],
magistrate = row['MagistrateJudge'],
incident_date = parse_str_date(row['DateofIncident']),
location = row['LocationListed'],
address = row['StreetAddress'],
city = row['City'],
state = row['State'],
lat = float(row['Latitude']) if row['Latitude'] else None,
lon = float(row['Longitude']) if row['Longitude'] else None,
census_place = row['CensusPlaceFips'],
census_msa = row['CensusMsaFips'],
census_met_div = row['CensusMetDivFips'],
census_mcd = row['CensusMcdFips'],
census_micro = row['CensusCbsaMicro'],
census_cbsa = row['CensusCbsaFips'],
census_block = row['CensusBlock'],
census_block_g = row['CensusBlockGroup'],
census_tract = row['CensusTract'],
census_county = row['CensusCountyFips'],
census_state = row['CensusStateFips'],
naaccr_cert = row['naaccrCertCode'],
m_number = row['MNumber'],
m_predirection = row['MPreDirectional'],
m_name = row['MName'],
m_suffix = row['MSuffix'],
m_city = row['MCity'],
m_state = row['MState'],
narrative = row['Narrative'],
primary_cause = row['primary_cause'],
federal_causes = row['federal_causes'],
state_causes = row['state_causes'],
interaction_type = row['interaction_type'],
officers = row['officers'],
victims = row['victims'],
misconduct_type = row['misconduct_type'],
weapons_used = row['weapons_used'],
outcome = row['outcome'],
tags = row['tags'],
reporter = row['EnteredBy'],
fact_checker = row['Fact-checkedby'],
differences = row['Differences'],
)
c.save()