Home > Software engineering >  CSV Column Bool convert issue
CSV Column Bool convert issue

Time:11-09

{
"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)   

But after that conversion I faced an error on retired bool type. Since the csv file is written in string format. All the converted bool comes with True because bool("true") or bool("false") is True, could you help me to convert all the retirement values into the correct bool type with using default system libraries (os, sys, time, json, csv,...)?

CodePudding user response:

To convert your string value to a bool, you should just be able to compare it as follows:

d[headers[16]] = reader_row[16].lower().strip() == 'true'

The lower() and strip() might not be needed but they ensure the string is in lowercase and doesn't have any extra spaces to aid the comparison.

  • Related