Reputation: 13
I am making a simple python script that will ask for a zipcode and return the weather implementing the latitude and longitude of that zipcode into a weather.gov url and the weather can be fetched. I am still a beginner so this might be a very simple fix. I will explain my code so far. I have done the Beautiful Soup stuff this is just implementing the zipcode to lat and long.
This imports the search engine:
from uszipcode import SearchEngine
This specifies simple zipcode:
search = SearchEngine(simple_zipcode=True)
This specifies the zipcode in question:
zipcode = search.by_zipcode("*****")
This takes the zipcode, gets it's data, turns it into a dict, and stores it into a variable:
latLong = zipcode.to_dict
This takes the lat and long from the dict and stores them in variables:
lat = latLong['lat']
long = latLong['long']
And finally, this puts the lat and long in the url for BeautifulSoup to do it's magic:
my_url = 'https://forecast.weather.gov/MapClick.php?lat=' + lat + '&lon=' + long + '#.XGTPH1xKiUk'
As you can see, I am using the uszipcode library. (Here's a link: https://uszipcode.readthedocs.io/?badge=latest)
Something else that might be important to note is that this is what you would get if you were to call the latLong variable:
<bound method ExtendedBase.to_dict of SimpleZipcode(zipcode='*****', zipcode_type='Standard', major_city='*****', post_office_city='******', common_city_list=['*****'], county='*****', state='****', lat=****, lng=****, timezone='****', radius_in_miles=*****, area_code_list=['***', '((('], population=***, population_density=*****, land_area_in_sqmi=***, water_area_in_sqmi=***, housing_units=******, occupied_housing_units=*****, median_home_value=*****, median_household_income=****, bounds_west=******, bounds_east=*****, bounds_north=*****, bounds_south=****)>
Ok so here's my problem: Whenever I try to call the method/values of variables in the dictionary, (aka the lat = latLong['lat']) this error message pops up:
Traceback (most recent call last):
File "c:\Users\bobko\Desktop\Hello\Test.py", line 8, in <module>
lat = zipcode.to_dict['lat']
TypeError: 'method' object is not subscriptable
I don't understand what I'm doing wrong. Can someone please help? Just a side note: I haven't implemented the input thing I am just seeing if this will even work.
Upvotes: 1
Views: 166
Reputation: 306
The below works for me.
You have to call the to_dict
method by adding parentheses and also the attribute is not "long" it is "lng"
from uszipcode import SearchEngine
search = SearchEngine(simple_zipcode=True)
zipcode = search.by_zipcode("11217")
latLong = zipcode.to_dict()
lat = latLong['lat']
long = latLong['lng']
Upvotes: 2
Reputation: 33335
zipcode.to_dict
is a method. You have to call it, i.e. zipcode.to_dict()
.
Without the parentheses, you're referring to the method instead of calling it.
Upvotes: 0
Reputation: 106553
You should call the method by placing parentheses after the method name:
latLong = zipcode.to_dict()
Otherwise you're only assigning the method object to latLong
, hence the error.
Upvotes: 1