{
"First Name": "Jonathan",
"Last Name": "Thomas",
"Marital Status": "married or civil partner",
"Sex": "Male",
"Age (Years)": 46,
"Retired": true,
"Distance Commuted to Work (miles)": 13.72,
"Employer Company": "Begum-Williams",
"Dependants": 1,
"Yearly Salary (\u00c2\u00a3)": 54016,
"Yearly Pension (\u00c2\u00a3)": 0,
"Address Street": {
"Address Street": "00 Wheeler wells",
"Address City": "Chapmanton",
"Address Postcode": "L2 7BT"
},
"Credit Card Number": {
"Credit Card Number": "4529436854129855",
"Credit Card Start Date": "08/12",
"Credit Card Expiry Date": "11/26",
"Credit Card CVV": 583,
"Bank IBAN": "GB37UMCO54540228728019"
},
"Vehicle Make": {
"Vehicle Make": "Nissan",
"Vehicle Model": "ATS",
"Vehicle Year": 1996,
"Vehicle Type": "Coupe"
}
I have customer data that headers order written randomly. The JSON requirement of the project is above order. So I write a code below to put the correct order and convert to types.
l = []
with open("userdata.csv", 'r') as data_file:
reader = csv.reader(data_file)
headers = next(reader)
for reader_row in reader:
d = {}
d[headers[11]] = str(reader_row[11])
d[headers[13]] = str(reader_row[13])
d[headers[14]] = str(reader_row[14])
d[headers[18]] = str(reader_row[18])
d[headers[3]] = int(reader_row[3])
d[headers[16]] = bool(reader_row[16]
d[headers[4]] = float(reader_row[4])
d[headers[5]] = str(reader_row[5])
d[headers[10]] = int(reader_row[10]) if reader_row[10] else None
d[headers[17]] = int(reader_row[17])
d[headers[15]] = int(reader_row[15])
d[headers[0]]={'Address Street': str(reader_row[0]),
'Address City': str(reader_row[1]),
'Address Postcode': str(reader_row[2])
}
d[headers[8]]={'Credit Card Number': str(reader_row[8]),
'Credit Card Start Date': str(reader_row[6]),
'Credit Card Expiry Date': str(reader_row[7]),
'Credit Card CVV': int(reader_row[9]),
'Bank IBAN' : str(reader_row[12])
}
d[headers[19]]={'Vehicle Make': str(reader_row[19]),
'Vehicle Model': str(reader_row[20]),
'Vehicle Year': int(reader_row[21]),
'Vehicle Type': str(reader_row[22])
}
l.append(d)
print(d)
Then I want to save a JSON file for any customers that have more than 10 years between their start and end date.
with open("remove_ccard.json", 'w', encoding='utf-8') as remove:
json.dump([d for d in l if d['Credit Card Expiry Date'[-2:]] - d['Credit Card Start Date'[-2:]] > 10], remove)
But I get an error on that line, could you help me to fix on my last code, or just guide me on which part I am writing wrong?
Thank you
CodePudding user response:
Use datetime instead
from datetime import datetime
#Replacing d['Credit Card Expiry Date'[-2:]] - d['Credit Card Start Date'[-2:]] > 10
start_date = datetime.strptime(d['Credit Card Start Date'],"%m/%y")
end_date = datetime.strptime(d['Credit Card Expiry Date'],"%m/%y")
When you subtract them you get the difference in days. The last part of your code will then be
with open("remove_ccard.json", 'w', encoding='utf-8') as remove:
json.dump([d for d in l if end_date - start_date > 10*365.25], remove)
CodePudding user response:
convert string to datetime
from datetime import datetime
start = "08/12"
end = "11/26"
date_time_obj1 = datetime.strptime(start, '%m/%y')
date_time_obj2 = datetime.strptime(end, '%m/%y')
c = date_time_obj1 - date_time_obj2