Related
I am using this python library to grab a response from the google places API.
Using the places function from the above library returns the object you see below. You can see that is just gives me a boolean on whether the restaurant is open or closed. How can I see the hours it is open for each day of the week? If this library isnt capable of it, can anyone show me an example that is?
Here is the line of code making the request for full context.
import googlemaps # https://googlemaps.github.io/google-maps-services-python/docs/index.html
gmaps = googlemaps.Client(key='apiKey')
response = gmaps.places(query=charlestonBars[idx], location=charleston)
[ { 'business_status': 'OPERATIONAL',
'formatted_address': '467 King St, Charleston, SC 29403, United States',
'geometry': { 'location': {'lat': 32.7890988, 'lng': -79.9386229},
'viewport': { 'northeast': { 'lat': 32.79045632989271,
'lng': -79.93725907010727},
'southwest': { 'lat': 32.78775667010727,
'lng': -79.93995872989272}}},
'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/bar-71.png',
'icon_background_color': '#FF9E67',
'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/bar_pinlet',
'name': "A.C.'s Bar & Grill",
------->'opening_hours': {'open_now': True},
'photos': [ { 'height': 1816,
'html_attributions': [ '<a '
'href="https://maps.google.com/maps/contrib/106222166168758671498">Lisa '
'Royce</a>'],
'photo_reference': 'ARywPAKpP_eyNL_y625xWYrQvSjAI91TzEx4XgT1rwCxjFyQjEAwZb2ha9EgE2RcKJalrZhjp0yTWa6QqvPNU9c7GeNBTDtzXVI0rHq2RXtTySGu8sjcB76keFugOmsl1ix4NnDVh0NO0vt_PO3nIZ-R-ytOzzIRhJgPAJd3SxKNQNfEyVp5',
'width': 4032}],
'place_id': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
'plus_code': { 'compound_code': 'Q3Q6+JG Charleston, South Carolina',
'global_code': '8742Q3Q6+JG'},
'price_level': 1,
'rating': 4.3,
'reference': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
'types': [ 'bar',
'restaurant',
'point_of_interest',
'food',
'establishment'
What you need to use is the Place Details of Places API
Before we proceed with the solution, we must understand that there's a difference in the result of a Place Search and a Place Details request.
According to the documentation:
"Place Search requests and Place Details requests do not return the same fields. Place Search requests return a subset of the fields that are returned by Place Details requests. If the field you want is not returned by Place Search, you can use Place Search to get a place_id, then use that Place ID to make a Place Details request."
Now what you used on your code according to the python library documentation is the places(*args, **kwargs) which is the Place Search.
The Google Maps API documentation you provided on your comment above where you can get the expected result of hours each day is from Place Details which is the place(*args, **kwargs) from the python library documentation.
As quoted above, to request the detail of a place, you need the place_id which you can get by doing a Places Search request like you did on your question. So what you only need to do is get the place_id of the location you want through Place Search, then use that place_id to get the Place Details result that includes the opening_hours field result.
Here's what it looks like on a python code:
# I used pprint to print the result on the console
from pprint import pprint
import googlemaps #import googlemaps python library
# Instantiate the client using your own API key
API_KEY = 'API_KEY'
map_client = googlemaps.Client(API_KEY)
# Store the location you want, in my case, I tried using 'Mall of Asia'
location_name = 'Mall of Asia'
# Instantiate Place Search request using `places(*args, **kwargs)` from the library
# Use the `stored location` as an argument for query
place_search = map_client.places(location_name)
# Get the search results
search_results = place_search.get('results')
# Store the place_id from the result to be used
place_id = (search_results[0]['place_id'])
# Instantiate Place Details request using the `place(*args, **kwargs)` from the library
# Use the stored place_id as an argument for the request
place_details = map_client.place(place_id)
# Get the Place Details result
details_results = place_details.get('result')
# Print the result specifying what you only need which is the `opening_hours` field
pprint(details_results['opening_hours'])
The result of this sample request would be this:
{'open_now': False,
'periods': [{'close': {'day': 0, 'time': '2200'},
'open': {'day': 0, 'time': '1000'}},
{'close': {'day': 1, 'time': '2200'},
'open': {'day': 1, 'time': '1000'}},
{'close': {'day': 2, 'time': '2200'},
'open': {'day': 2, 'time': '1000'}},
{'close': {'day': 3, 'time': '2200'},
'open': {'day': 3, 'time': '1000'}},
{'close': {'day': 4, 'time': '2200'},
'open': {'day': 4, 'time': '1000'}},
{'close': {'day': 5, 'time': '2200'},
'open': {'day': 5, 'time': '1000'}},
{'close': {'day': 6, 'time': '2200'},
'open': {'day': 6, 'time': '1000'}}],
'weekday_text': ['Monday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Tuesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Wednesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Thursday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Friday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Saturday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
'Sunday: 10:00\u202fAM\u2009–\u200910:00\u202fPM']}
That would be all and I hope this helps! Feel free to comment below if this does not meet your expected results.
I'm trying to assign two values of a list to two different variables. Here is the json list. Its raising key error. Please let me know where I am wrong.
[{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
Here is my code python3 code:
pt = [{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
y = [item[0] for item in pt]
z = [item[0] for item in pt]
print(y)
print(z)
Error:
File "test_YourM.py", line 19, in <module>
y = [item[0][0] for item in pt] File "test_YourM.py", line 19, in <listcomp>
y = [item[0][0] for item in pt] KeyError: 0
Expected output:
print(y) # {'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0}
print(z) # {'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}
[item for item in pt[0]]
[item for item in pt[1]]
The item is generated in in that scope, while pt isn't, even though you're enumerating a dict, you may want to do something like this:
{key: value for key, value in pt[0].items()}
{key: value for key, value in pt[1].items()}
I have this string:
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
If I do:
type(string)
I get:
<type 'unicode'>
If I do:
type(firstString)
where firstString is just the first of the three parts of the string, I get:
<type 'unicode'>
With Python, how can I split it based on the external parentheses, such that from this one string, we obtain three strings, each one having the form "{ ... }"?
If there are no nested objects and newlines are not guaranteed between parts, as you've said, it's as simple as splitting on }:
your_string = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''
substrings = [part.strip() + "}" for part in your_string.split("}") if part.strip()]
# ['{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
# "low":"265.6", "prevClose":"266.75","close":"265.66"}',
# '{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
# "bidexch":"Q","biddate":"1513293904000","ask":265.42,
# "asksz":45,"askexch":"P","askdate":"1513294015000"}',
# '{"type":"summary","symbol":"SPY","open":"267.09",
# "high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}']
Or you can immediately parse the parts into individual Python dictionaries:
dicts = [json.loads(part + "}") for part in your_string.split("}") if part.strip()]
# [{'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
# 'close': '265.66', 'low': '265.6', 'symbol': 'SPY'},
# {'askdate': '1513294015000', 'bid': 265.38, 'asksz': 45, 'type': 'quote',
# 'ask': 265.42, 'bidsz': 3, 'bidexch': 'Q', 'biddate': '1513293904000',
# 'askexch': 'P', 'symbol': 'SPY'},
# {'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
# 'close': '265.66', 'low': '265.6', 'symbol': 'SPY'}]
You can split the string on the newline character and parse it using the json module.
import json
s = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,"bidexch":"Q","biddate":"1513293904000","ask":265.42,"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''
[json.loads(line) for line in s.split('\n')]
# returns:
[{'close': '265.66',
'high': '267.22',
'low': '265.6',
'open': '267.09',
'prevClose': '266.75',
'symbol': 'SPY',
'type': 'summary'},
{'ask': 265.42,
'askdate': '1513294015000',
'askexch': 'P',
'asksz': 45,
'bid': 265.38,
'biddate': '1513293904000',
'bidexch': 'Q',
'bidsz': 3,
'symbol': 'SPY',
'type': 'quote'},
{'close': '265.66',
'high': '267.22',
'low': '265.6',
'open': '267.09',
'prevClose': '266.75',
'symbol': 'SPY',
'type': 'summary'}]
I have some text data
>>> print content
Date,Open,High,Low,Close,Volume,Adj Close
2015-03-17,4355.83,4384.98,4349.69,4375.63,1724370000,4375.63
2015-03-16,4338.29,4371.46,4327.27,4370.47,1713480000,4370.47
2015-03-13,4328.09,4347.87,4289.30,4314.90,1851410000,4314.90
2015-03-12,4302.73,4339.20,4300.87,4336.23,1855110000,4336.23
2015-03-11,4336.05,4342.87,4304.28,4305.38,1846020000,4305.38
Now I want to convert this into a Dict, so that I can load this into a database using the cursor.executemany that allows me to provide dict as an input.
Is there a module to auto convert this into a Dict. I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that i can do this without creating a file?
Use csv.DictReader
>>> with open('text.txt') as f:
... dreader = csv.DictReader(f)
... for row in dreader:
... print(row)
...
{'Adj Close': '4375.63', 'High': '4384.98', 'Volume': '1724370000', 'Low': '4349.69', 'Close': '4375.63', 'Open': '4355.83', 'Date': '2015-03-17'}
{'Adj Close': '4370.47', 'High': '4371.46', 'Volume': '1713480000', 'Low': '4327.27', 'Close': '4370.47', 'Open': '4338.29', 'Date': '2015-03-16'}
{'Adj Close': '4314.90', 'High': '4347.87', 'Volume': '1851410000', 'Low': '4289.30', 'Close': '4314.90', 'Open': '4328.09', 'Date': '2015-03-13'}
{'Adj Close': '4336.23', 'High': '4339.20', 'Volume': '1855110000', 'Low': '4300.87', 'Close': '4336.23', 'Open': '4302.73', 'Date': '2015-03-12'}
{'Adj Close': '4305.38', 'High': '4342.87', 'Volume': '1846020000', 'Low': '4304.28', 'Close': '4305.38', 'Open': '4336.05', 'Date': '2015-03-11'}
I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that I can do this without creating a file?
You can use a file like object if you do not want to have physical data.
Use tempfile.TemporaryFile
from tempfile import TemporaryFile
with TemporaryFile('w+t') as flike:
flike.write(content)
flike.seek(0)
dreader = csv.DictReader(flike)
for row in dreader:
#do something
Use io.StringIO
import io #python3 or import StringIO in python2
flike = io.StringIO(content)
for row in csv.DictReader(flike)
#do something
I'm building API for my app in Python and Flask. I'm trying to output some data in JSON format, however I get weirdly formatted data from my sql query. I would like to see numbers (i.e. field initial_price) as number, not as a Decimal('3.99'), similarly with id and timestamp format.
Also, is it the right way to produce JSON?
This is output from my API:
$ curl 127.0.0.1:5000/1/product?code="9571%2F702"
[{'update_time': datetime.datetime(2013, 1, 7, 22, 25, 50), 'code': '9571/702', 'description': '', 'gender': '', 'brand': 'Zara', 'initial_price': Decimal('3.99'), 'image_link': 'http://static.zara.net/photos//2012/I/0/3/p/9571/702/401/9571702401_1_1_3.jpg', 'currency': 'GBP', 'colors': 'NAVY', 'link': 'http://www.zara.com/webapp/wcs/stores/servlet/product/uk/en/zara-neu-W2012-s/341501/883021/', 'current_price': Decimal('990.00'), 'original_category': 'Girl (2-14 years)', 'id': 1623L, 'name': '"I LOVE ..." T-SHIRT'},
{'update_time': datetime.datetime(2013, 1, 7, 22, 25, 50), 'code': '9571/702', 'description': '', 'gender': '', 'brand': 'Zara', 'initial_price': Decimal('3.99'), 'image_link': 'http://static.zara.net/photos//2012/I/0/3/p/9571/702/401/9571702401_1_1_3.jpg', 'currency': 'GBP', 'colors': 'LIGHT', 'link': 'http://www.zara.com/webapp/wcs/stores/servlet/product/uk/en/zara-neu-W2012-s/341501/883021/', 'current_price': Decimal('990.00'), 'original_category': 'Girl (2-14 years)', 'id': 1624L, 'name': '"I LOVE ..." T-SHIRT'},
{'update_time': datetime.datetime(2013, 1, 7, 22, 25, 50), 'code': '9571/702', 'description': '', 'gender': '', 'brand': 'Zara', 'initial_price': Decimal('3.99'), 'image_link': 'http://static.zara.net/photos//2012/I/0/3/p/9571/702/401/9571702401_1_1_3.jpg', 'currency': 'GBP', 'colors': 'ECRU', 'link': 'http://www.zara.com/webapp/wcs/stores/servlet/product/uk/en/zara-neu-W2012-s/341501/883021/', 'current_price': Decimal('990.00'), 'original_category': 'Girl (2-14 years)', 'id': 1625L, 'name': '"I LOVE ..." T-SHIRT'}]
My code is as following:
from flask import Flask, url_for, session, redirect, escape, request
from subprocess import Popen, PIPE
import socket
import MySQLdb
import urllib
#app.route('/1/product')
def product_search():
[some not important stuff here...]
#creating list of codes with lowest Damerau-Levenshtein numbers
best_matching_codes = []
for k, v in lv:
if v == min:
best_matching_codes.append(k)
#returning JSON with best matching products info
products_json = []
for code in best_matching_codes:
cur = db.cursor()
query = "SELECT * FROM %s WHERE code LIKE '%s'" % (PRODUCTS_TABLE_NAME, product_code)
cur.execute(query)
columns = [desc[0] for desc in cur.description]
rows = cur.fetchall()
for row in rows:
products_json.append(dict((k,v) for k,v in zip(columns,row)))
return str(products_json)
Use the json module. It will output proper json from a dict:
import json
return json.dumps(products_json)
Using str does not produce valid json!
You need to use a json library to produce json.
Add to top:
import json
Change last line to:
return json.dumps(products_json)