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 agerangeage:
age intage
if age :
return
elif age :
return
elif age :
return
elif age :
return
else:
return "NOAGE"
def timeofdaydatetimestr:
t datetime.strptimedatetimestrYmd H:M:S
th thour
if th :
return "morning"
elif th :
return "afternoon"
elif th :
return "evening"
else:
return "NONE"
:type csvcontent: String
:rtype: string
def mydatatransformcsvcontent:
# Split rows into an array
rows csvcontent.split
# Extract headers and data rows
headers rows.pop# Remove and capture the first row headers
transformedrows headers# Initialize transformed rows with headers
# Process each row
for row in rows:
fields row.split
# Apply transformations
fields fieldssplit@ # # Remove username from email
fields agerangefields# Transform age to range
fields timeofdayfields# Transform order time to time of day
# Rejoin fields and add to transformed rows
transformedrows.appendjoinfields
return transformedrows
printmydatatransformGenderFirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
Male,Carl,Wilderman,carl,wildermancarl@yahoo.com,Seattle,Safari iPhone,::
Male,Marvin,Lind,marvin,marvinlind@hotmail.com,Detroit,Chrome Android,::
Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,Las Vegas,Chrome,::
Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,Seattle,Chrome,::
Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,Chicago,Chrome Android,::
# input:
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wildermancarl@yahoo.com,Seattle,Safari iPhone,::
# Male,Marvin,Lind,marvin,marvinlind@hotmail.com,Detroit,Chrome Android,::
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,Las Vegas,Chrome,::
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,Seattle,Chrome,::
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,Chicago,Chrome Android,::
# expected output return with spaces between rows:
# GenderFirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At "Male,Carl,Wilderman,carl,yahoo.com,Seattle,Safari iPhone,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,Detroit,Chrome Android,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,Las Vegas,Chrome,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,Seattle,Chrome,morning", "Male,Derick,McLaughlin,derick,hotmail.com,Chicago,Chrome Android,afternoon"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
