Question: Create a function in Python that will read CSV data ( without using import csv ) , modify fields, and return an array in CSV

Create a function in Python that will read CSV data (without using import csv), modify fields, and return an array in CSV format with the columns Email, Age, and Order At transformed (note the space between rows in the output):
from datetime import datetime
def age_range(age):
age = int(age)
if 1<= age <=20:
return "1->20"
elif 21<= age <=40:
return "21->40"
elif 41<= age <=65:
return "41->65"
elif 66<= age <=99:
return "66->99"
else:
return "NOAGE"
def time_of_day(datetime_str):
t = datetime.strptime(datetime_str,'%Y-%m-%d %H:%M:%S')
th = t.hour
if 6<= th <12:
return "morning"
elif 12<= th <18:
return "afternoon"
elif 18<= th <24:
return "evening"
else:
return "NONE"
"""
:type csv_content: {String}
:rtype: string[]
"""
def my_data_transform(csv_content):
# Split rows into an array
rows = csv_content.split("
")
# Extract headers and data rows
headers = rows.pop(0)# Remove and capture the first row (headers)
transformed_rows =[headers]# Initialize transformed rows with headers
# Process each row
for row in rows:
fields = row.split(",")
# Apply transformations
fields[4]= fields[4].split("@") # [1]# Remove username from email
fields[5]= age_range(fields[5])# Transform age to range
fields[9]= time_of_day(fields[9])# Transform order time to time of day
# Rejoin fields and add to transformed rows
transformed_rows.append(",".join(fields))
return transformed_rows
print(my_data_transform("Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
Male,Carl,Wilderman,carl,wilderman_carl@yahoo.com,29,Seattle,Safari iPhone,2,2020-03-0616:37:56
Male,Marvin,Lind,marvin,marvin_lind@hotmail.com,77,Detroit,Chrome Android,2,2020-03-0213:55:51
Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,21,Las Vegas,Chrome,1,2020-03-0517:53:05
Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,81,Seattle,Chrome,2,2020-03-0410:33:53
Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,47,Chicago,Chrome Android,1,2020-03-0515:19:48
"))
# input:
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wilderman_carl@yahoo.com,29,Seattle,Safari iPhone,2,2020-03-0616:37:56
# Male,Marvin,Lind,marvin,marvin_lind@hotmail.com,77,Detroit,Chrome Android,2,2020-03-0213:55:51
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,21,Las Vegas,Chrome,1,2020-03-0517:53:05
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,81,Seattle,Chrome,2,2020-03-0410:33:53
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,47,Chicago,Chrome Android,1,2020-03-0515:19:48
")
# expected output (return, with spaces between rows):
# ["Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At", "Male,Carl,Wilderman,carl,yahoo.com,21->40,Seattle,Safari iPhone,2,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,66->99,Detroit,Chrome Android,2,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,21->40,Las Vegas,Chrome,1,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,66->99,Seattle,Chrome,2,morning", "Male,Derick,McLaughlin,derick,hotmail.com,41->65,Chicago,Chrome Android,1,afternoon"]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!