Geocoding a list of addresses from a CSV file - python

I am trying to geocode a list of addresses in a CSV file. The column with the information is named "full" and it looks like this:
full
100 Ross St,15219
1014 Blackadore Ave.,15208
1026 Winterton St.,15206
...
This is the code that I am using:
import csv
import pygeocoder
import pandas as pd
from pygeocoder import Geocoder
df = pd.read_csv('C:\Users\Jesus\Desktop\Events.csv')
address = df.full
result = Geocoder.geocode(address)
print(result[0].coordinates)
And this is the output:
Traceback (most recent call last):
File "C:\Users\Jesus\Desktop\python\geocode.py", line 10, in <module>
result = Geocoder.geocode(address)
File "C:\Python27\lib\site-packages\pygeocoder.py", line 160, in geocode
return GeocoderResult(Geocoder.get_data(params=params))
File "C:\Python27\lib\site-packages\pygeocoder.py", line 107, in get_data
response_json = response.json()
File "C:\Python27\lib\site-packages\requests\models.py", line 693, in json
return json.loads(self.text, **kwargs)
File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 488, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 389, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 122, in scan_once
return _scan_once(string, idx)
File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 118, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
JSONDecodeError: Expecting value: line 1 column 1 (char 0

Your address variable is a Series object from pandas, which may be causing this problem. To geocode all the addresses from the CSV, iterate through it like:
for a in address:
result = Geocoder.geocode(a)
print(result[0].coordinates)
To store the results in a file (assuming Python 2.x):
with open('filename', 'w') as outfile:
for a in address:
result = Geocoder.geocode(a)
print >>outfile, str(result[0].coordinates) # Prints to file
You can do outfile.write( str(result[0].coordinates) ) instead of the print if you want. The only difference is that print automatically adds a line break. To add to a list, just declare the list outside of your for statement (such as coordinates_list = []) and replace the print with coordinates_list.append(result[0].coordinates). Either of these ways will work in Python 3.x, but the print >>outfile statement will not.

Related

json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 57358 (char 57357)

I am getting a JSON response from the server and storing it in a text file
Later I parse certain data from the text file, but while doing so I am getting the above error. The JSON format seems to be okay to me Can you please help me here. Thanks Also I do not have any control over the response I receive from the server. For now, I am just using dump to write it to a text file
Code for json dump and load
balance = exchange.fetch_balance()
#print(balance)
with open('balance.txt', 'w') as json_file:
json.dump(balance, json_file)
print("Balance file created")
with open('balance.txt') as json_file:
data = json.load(json_file)
freeusdt = float(data['info']['balances'][11]['free'])
Qty=float(df['close'][last_row_index])
Qty=Qty+300
buyqty=freeusdt/Qty
My JSON file is here (in the .txt file Since StackOverflow won't let me post the full code as it exceeds the character limit)
https://drive.google.com/file/d/1kpBMl6JbzlH9OQvvteLl0Ieb2PaBU_kr/view?usp=sharing
The full error is
Traceback (most recent call last):
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 232, in <module>
schedule.run_pending()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 780, in run_pending
default_scheduler.run_pending()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 100, in run_pending
self._run_job(job)
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 172, in _run_job
ret = job.run()
File "C:\Users\pavan.alur\Miniconda3\lib\site-packages\schedule\__init__.py", line 661, in run
ret = self.job_func()
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 223, in run_bot
check_buy_sell_signals(supertrend_data)
File "C:\Users\pavan.alur\Desktop\SUPERTRENDWITHKAMA\supertrendwithkama.py", line 121, in check_buy_sell_signals
data1 = json.load(json_file1)
File "C:\Users\pavan.alur\Miniconda3\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\pavan.alur\Miniconda3\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\pavan.alur\Miniconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\pavan.alur\Miniconda3\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 57358 (char 57357)
The indentation problem as mentioned by #MattDMo solved it
balance = exchange.fetch_balance()
with open('balance.txt', 'w') as json_file:
json.dump(balance, json_file)
print("Balance file created")
with open('balance.txt') as json_file:
data = json.load(json_file)
freeusdt = float(data['info']['balances'][11]['free'])
Qty=float(df['close'][last_row_index])
Qty=Qty+300
buyqty=freeusdt/Qty

Unable to read API responce data

I am trying to get data from the below seen URLs
import requests
import json
service_a = requests.get('https:xxxxx')
service_b = requests.get('https:xxxxx')
service_a.json()
service_b.json()
While for the case of service_a, service_a.json() gives me the expected data, for service_b, the call service_b.json() gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 79, in scan_once
return _scan_once(string, idx)
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 70, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Why is this happening?
ps. I notced that when I try to visit the https link of service_a, the data it contains are shown in the browser, while for sevice_b, no data are shown in the browser, but when I click it a file with its data is downloaded in my computer.
Try to check the Content-Type of service_b response.
import csv
response = requests.get(API_URL)
decoded_data = response.content.decode("utf-8")
csv_data = csv.reader(decoded_data.splitlines(), delimiters=",")
csv_list = list(csv_data)
in most cases your
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
error is due to:
non-JSON conforming quoting
XML/HTML output (that is, a string starting with <), or
incompatible character encoding

JSON decode error since I switched from macOS to Windows

I have a Python file that worked perfectly on my macbook.
Once I moved it to window, for some reason it gave me new error.
this is the errors im getting:
Traceback (most recent call last):
File "C:/Users/chadi/PycharmProjects/untitled4/main.py", line 3, in <module>
from main_controller import Ui_MainController
File "C:\Users\chadi\PycharmProjects\untitled4\main_controller.py", line 6, in <module>
from get_companies import load_companies
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 10, in <module>
json_read('convertcsv.json')
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 8, in json_read
data = (json.load(f_in))
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Process finished with exit code 1
this is all the parts where i load a JSON file:
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
json_read('convertcsv.json')
def mark_employee_unavailability(service,employee,reason,start_date,end_date):
with open('info_inspecteurs.json') as json_file:
data = json.load(json_file)
for i in range(len(data)):
if data[i]['n_inspecteur'] == employee:
event_email = data[i]['email_inspecteur']
break
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
def get_employee_info(n_inspecteur):
json_read('info_inspecteurs.json')
value = list(filter(lambda x: x["n_inspecteur"] == n_inspecteur, data))[0]
if len(value) > 0:
print(value['ad_inspecteur'], "\n", value['email_inspecteur'])
return (value['ad_inspecteur'],value['email_inspecteur'])
print("no record found")
return None
def load_employees_from_info_inspecteurs():
json_read('info_inspecteurs.json')
employees=[]
for company in data:
employees.append(company['n_inspecteur'])
return employees
I don't know where it came from, or maybe is the new environement I used on Windows? I used anaconda. Does it change anything? I was on VENV on my macOS.
Thank you for your help
The error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
may just mean that there's no data being read from the target file at all, so json isn't finding any data to parse.
You might troubleshoot by trying to just read the file from that point in your script into a string - if it fails, the problem may be in python execution current directory / relative file path, rather than in the json parsing.
with open ("convertcsv.json", "r") as checkFile:
checkData = checkFile.read()
# print out length or contents of checkData or sim.

Reading JSON file using Python

I have a JSON file called 'elements.json':
[
{ldraw="003238a",lgeo="003238a",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238b",lgeo="003238b",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238c",lgeo="003238c",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238d",lgeo="003238d",slope=0,anton=0,lutz=0,owen=0,damien=0}
]
I have a Python file called 'test.py':
import json
with open('elements.json') as json_file:
data = json.load(json_file)
for p in data:
print('ldraw: ' + p['ldraw'])
print('lgeo: ' + p['lgeo'])
Running from the Windows command line I get this error:
Traceback (most recent call last):
File "test.py", line 4, in <module>
data = json.load(json_file)
File "C:\Python27\lib\json\__init__.py", line 278, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 2 column 2 (char 3)
What property name is expected? Why am I getting the error?
You aren't following the JSON specification. See json.org for details.
[
{"ldraw":"003238a","lgeo":"003238a","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238b","lgeo":"003238b","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238c","lgeo":"003238c","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238d","lgeo":"003238d","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0}
]
Your Python code is correct.
Your ldraw and lgeo values look like hexadecimal; JSON does not support hex, and you will have to do the extra work yourself.
[Edit: They're not]
Your file elements.json is not a valid json file.
It should have looked like this -
[{"ldraw":"003238a","lgeo":"003238a"}]
Your JSON format is invalid, JSON stands for JavaScript Object Notation, like the Javascript Object. So, you should replace "=" to ":". It means key-value pairs.
Wrong:
ldraw="003238a"
ldraw: 003238a // if no quote, the value should be the digit only.
Right:
ldraw: "003238a"
ldraw: { "example-key": "value" }
ldraw: "True"

Creating dataframe from json not always working

I'm trying to run this code to create a data frame from a JSON link. Sometimes, the code will run. Other times, I will get an error message (below). I'm not sure why this occurs, even though the code is the same.
import requests
import json
url = "http://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
jd = requests.get(url).json()
df = []
for item in requests.get(url).json()['resultSets']:
print("got here")
row_df = []
for row in item['rowSet']:
row_df.append(str(row).strip('[]'))
df.append("\n")
df.append(row_df)
print(df)
Error Message:
Traceback (most recent call last):
File "/Users/K/PycharmProjects/mousefun/fun", line 8, in <module>
jd = requests.get(url).json()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 812, in json return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Change your request logic to this and try again:
r = requests.get(url)
r.raise_for_status()
df = []
for item in r.json()["resultSets"]:
# ...
r.raise_for_status() will raise if the status is not OK .
Also, this does not do the request two times like your code does.

Categories

Resources