"""
Your task is to check the "productionStartYear" of the DBPedia autos datafile for valid values.
The following things should be done:
- check if the field "productionStartYear" contains a year
- check if the year is in range 1886-2014
- convert the value of the field to be just a year (not full datetime)
- the rest of the fields and values should stay the same
- if the value of the field is a valid year in range, as described above,
  write that line to the output_good file
- if the value of the field is not a valid year, 
  write that line to the output_bad file
- discard rows (neither write to good nor bad) if the URI is not from dbpedia.org
- you should use the provided way of reading and writing data (DictReader and DictWriter)
  They will take care of dealing with the header.

You can write helper functions for checking the data and writing the files, but we will call only the 
'process_file' with 3 arguments (inputfile, output_good, output_bad).
"""
import csv
from pprint import pprint
from datetime import datetime

INPUT_FILE = 'autos.csv'
OUTPUT_GOOD = 'autos-valid.csv'
OUTPUT_BAD = 'FIXME-autos.csv'

def process_file(input_file, output_good, output_bad):

    good_data= []
    bad_data=[]

    with open(input_file, "r") as f:
        reader = csv.DictReader(f)
        header = reader.fieldnames

        #separate the data into good and bad
        for idx, row in enumerate(reader):
            if idx < 3: continue
            if row["URI"].find("dbpedia.org") == -1: continue 
            #convert year to int
            year = row["productionStartYear"]
            if type(year) == str:
                if year == "NULL":
                    row["productionStartYear"] = 0 
                else:
                    row["productionStartYear"] = int(year[:4])
            # check the range and save to appropriate collection
            if row["productionStartYear"] > 1885 and row["productionStartYear"] < 2015:
                good_data.append(row)
            else:
                bad_data.append(row)

    with open(output_good, "w") as g:
        writer = csv.DictWriter(g, delimiter=",", fieldnames= header)
        writer.writeheader()
        for row in good_data:
            writer.writerow(row)

    with open(output_bad, "w") as g:
        writer = csv.DictWriter(g, delimiter=",", fieldnames=header)
        writer.writeheader()
        for row in bad_data:
            writer.writerow(row)

def test():
    process_file(INPUT_FILE, OUTPUT_GOOD, OUTPUT_BAD)

if __name__ == "__main__":
    test()
"""
Your task is to check the "productionStartYear" of the DBPedia autos datafile for valid values.
The following things should be done:
- check if the field "productionStartYear" contains a year
- check if the year is in range 1886-2014
- convert the value of the field to be just a year (not full datetime)
- the rest of the fields and values should stay the same
- if the value of the field is a valid year in range, as described above,
  write that line to the output_good file
- if the value of the field is not a valid year, 
  write that line to the output_bad file
- discard rows (neither write to good nor bad) if the URI is not from dbpedia.org
- you should use the provided way of reading and writing data (DictReader and DictWriter)
  They will take care of dealing with the header.

You can write helper functions for checking the data and writing the files, but we will call only the 
'process_file' with 3 arguments (inputfile, output_good, output_bad).
"""
import csv
from pprint import pprint
from datetime import datetime

INPUT_FILE = 'autos.csv'
OUTPUT_GOOD = 'autos-valid.csv'
OUTPUT_BAD = 'FIXME-autos.csv'

def process_file(input_file, output_good, output_bad):

    good_data= []
    bad_data=[]

    with open(input_file, "r") as f:
        reader = csv.DictReader(f)
        header = reader.fieldnames

        #separate the data into good and bad
        for idx, row in enumerate(reader):
            if idx < 3: continue
            if row["URI"].find("dbpedia.org") == -1: continue 
            #convert year to int
            year = row["productionStartYear"]
            if type(year) == str:
                if year == "NULL":
                    row["productionStartYear"] = 0 
                else:
                    row["productionStartYear"] = int(year[:4])
            # check the range and save to appropriate collection
            if row["productionStartYear"] > 1885 and row["productionStartYear"] < 2015:
                good_data.append(row)
            else:
                bad_data.append(row)

    with open(output_good, "w") as g:
        writer = csv.DictWriter(g, delimiter=",", fieldnames= header)
        writer.writeheader()
        for row in good_data:
            writer.writerow(row)

    with open(output_bad, "w") as g:
        writer = csv.DictWriter(g, delimiter=",", fieldnames=header)
        writer.writeheader()
        for row in bad_data:
            writer.writerow(row)

def test():
    process_file(INPUT_FILE, OUTPUT_GOOD, OUTPUT_BAD)

if __name__ == "__main__":
    test()
Your task is to check the "productionStartYear" of the DBPedia autos datafile for valid values. The following things should be done: - check if the field "productionStartYear" contains a year - check if the year is in range 1886-2014 - convert the value of the field to be just a year (not full datetime) - the rest of the fields and values should stay the same - if the value of the field is a valid year in range, as described above,   write that line to the output_good file - if the value of the field is not a valid year,    write that line to the output_bad file - discard rows (neither write to good nor bad) if the URI is not from dbpedia.org - you should use the provided way of reading and writing data (DictReader and DictWriter)   They will take care of dealing with the header.  You can write helper functions for checking the data and writing the files, but we will call only the  'process_file' with 3 arguments (inputfile, output_good, output_bad). #separate the data into good and bad #convert year to int # check the range and save to appropriate collection