PYTHON - I need help determining exactly what is happening in both codes. 2 desc
ID: 3819775 • Letter: P
Question
PYTHON - I need help determining exactly what is happening in both codes. 2 descriptions
1st code:
for record in parsed_data:
date = record['Date']
year = date[-4:]
month = date[:date.index('/')]
day = date[date.index('/')+1:-5]
d = datetime.date(int(year),int(month),int(day))
dayOfWeek = d.weekday()
dayList = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
record['DayOfWeek']=dayList[dayOfWeek]
print(new_data[0])
2nd code:
geolocator = Nominatim()
for record in parsed_data:
street = record['StreetName']+' '+record['Type']
location = geolocator.geocode(street + " HOUSTON TX")
if location != None:
record['Y'] = location.latitude
record['X'] = location.longitude
else:
# set coordinates to 0,0 and then make sure to ignore those
record['Y'] = '0'
record['X'] = '0'
print(parsed_data[0])
Explanation / Answer
1st code:
for record in parsed_data:
date = record['Date'] # get date value from record date, in mm/dd/yyyy format
year = date[-4:] # last 4 char from end is year
month = date[:date.index('/')] # get month from date, from begining to first '/'
day = date[date.index('/')+1:-5] # get day from date, from first '/' to 5th last char from end
d = datetime.date(int(year),int(month),int(day)) # create date using year month and date
dayOfWeek = d.weekday() # Return the day of the week as an integer, where Monday is 0 and Sunday is 6
dayList = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
record['DayOfWeek']=dayList[dayOfWeek] #insert dayname into record to corresponding day
print(new_data[0])
geolocator = Nominatim()
for record in parsed_data: # for each parsed data
street = record['StreetName']+' '+record['Type'] # generate street name
location = geolocator.geocode(street + " HOUSTON TX") # call this function to get location
if location != None: # if location exist, insert it into record as latitude and longitude position
record['Y'] = location.latitude
record['X'] = location.longitude
else: # if location doesnot exist insert it into record as latitude and longitude position as 0
# set coordinates to 0,0 and then make sure to ignore those
record['Y'] = '0'
record['X'] = '0'
print(parsed_data[0])
>>> date='12/10/1991'
>>> import datetime
>>> y=date[-4:]
>>> y
'1991'
>>> m=date[:date.index('/')]
>>> m
'12'
>>> day = date[date.index('/')+1:-5]
>>> day
'10'
>>> d = datetime.date(int(y),int(m),int(day))
>>> d
datetime.date(1991, 12, 10)
>>> dayOfWeek = d.weekday()
>>> dayOfWeek
1
>>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.