I'm working on a simple time zone finder using Python3 and the Arrow library. I was working on it yesterday, and it had been up and running perfectly. I boot up today, though, and I'm getting this error: TypeError: expected string or bytes-like object
. Below is my code:
import googlemaps
from timezonefinder import TimezoneFinder
from dateutil import tz
import arrow as ar
gmaps = googlemaps.Client(key="GOOGLE_API")
lat = 0
lon = 0
tf = TimezoneFinder()
def timeZone(region):
global lat, lon
lat = 0
lon = 0
try:
lat = extract_values(gmaps.geocode(region), "latitude" or "lat")[1]
lon = extract_values(gmaps.geocode(region), "longitude" or "lng")[1]
except IndexError:
print("query incorrect. please try again.")
lat = 0
lon = 0
return(tf.timezone_at(lng=lon, lat=lat))
# Extracts all key values from a dictionary obj
def extract_values(obj, key):
"""Pull all values of specified key from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
results = extract(obj, arr, key)
return results
utc = ar.utcnow()
location = input("Enter a city for the time zone it's in. ")
region = timeZone(location)
print(region) # returns None
shifted = utc.to(region)
formatted = shifted.format("HH:mm:ss")
print("time in", location, "is:", formatted)
I know that my location
variable is of class string, I checked the type. Any ideas? I've spent ages looking around for a solution and can't find anything. Thanks!
EDIT: Traceback:
Traceback (most recent call last):
File "timezoneArrow.py", line 51, in <module>
shifted = utc.to(region)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/arrow/arrow.py", line 718, in to
tz = parser.TzinfoParser.parse(tz)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/arrow/parser.py", line 548, in parse
iso_match = cls._TZINFO_RE.match(tzinfo_string)
TypeError: expected string or bytes-like object
Well, I fixed it, but I'm not sure how. I just rebuilt my code from the ground up, using the same commands, and it worked! Wish I could give a better solution.