Querying issue with SPARQLWrapper - json

I am pretty new with SPARQL query using python package SPARQLWrapper. I was trying to retrieve the results from the DBpedia using following query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?s ?p
WHERE {
?country a type:LandlockedCountries ;
rdfs:label ?s ;
prop:populationEstimate ?p .
FILTER (?p > 15000000) .
}
My code written with Python 2.7 is as follows:
from SPARQLWrapper import SPARQLWrapper, JSON, POST
import sys
def main(argv):
endpoint = str("http://dbpedia.org/sparql")
# QUERY as mentioned above
query = str(QUERY)
query = query.replace("__oc__","{")
query = query.replace("__ob__","[")
query = query.replace("__cc__","}")
query = query.replace("__cb__","]")
query = query.replace("__cr__"," ")
query = query.replace("__cn__"," ")
print "Parsed Query: " + query
sparql = SPARQLWrapper(endpoint)
sparql.setQuery(query)
sparql.setMethod(POST)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
render = str("html")
if render == "html":
html_render(results)
else:
tab_render(results)
def html_render(results):
for result in results["results"]["bindings"]:
print result["s"]["value"], result["p"]["value"]
def tab_render(results):
for result in results["results"]["bindings"]:
print result["s"]["value"], result["p"]["value"]
if __name__ == '__main__':
main(sys.argv)
I am suppose to receive the name of a bunch of country name and it's population. However, I am getting only one result that is:
Afghanistan 31822848
Am I doing something wrong? Any kind of help would be highly appreciated.

Related

How use wikidata api to access to the statements

I'm trying to get information from Wikidata. For example, to access to "cobalt-70" I use the API.
API_ENDPOINT = "https://www.wikidata.org/w/api.php"
query = "cobalt-70"
params = {
'action': 'wbsearchentities',
'format': 'json',
'language': 'en',
'search': query
}
r = requests.get(API_ENDPOINT, params = params)
print(r.json())
So there is a "claims" which gives access to the statements. Is there a best way to check if a value exists in the statement? For example, "cobalt-70" have the value 0.5 inside the property P2114. So how can I check if a value exists in the statement of the entity? As this example.
Is there an approach to access it. Thank you!
I'm not sure this is exactly what you are looking for, but if it's close enough, you can probably modify it as necessary:
import requests
import json
url = 'https://www.wikidata.org/wiki/Special:EntityData/Q18844865.json'
req = requests.get(url)
targets = j_dat['entities']['Q18844865']['claims']['P2114']
for target in targets:
values = target['mainsnak']['datavalue']['value'].items()
for value in values:
print(value[0],value[1])
Output:
amount +0.5
unit http://www.wikidata.org/entity/Q11574
upperBound +0.6799999999999999
lowerBound +0.32
amount +108.0
unit http://www.wikidata.org/entity/Q723733
upperBound +115.0
lowerBound +101.0
EDIT:
To find property id by value, try:
targets = j_dat['entities']['Q18844865']['claims'].items()
for target in targets:
line = target[1][0]['mainsnak']['datavalue']['value']
if isinstance(line,dict):
for v in line.values():
if v == "+0.5":
print('property: ',target[0])
Output:
property: P2114
I try a solution which consists to search inside the json object as the solution proposed here : https://stackoverflow.com/a/55549654/8374738. I hope it can help. Let's give you the idea.
import pprint
def search(d, search_pattern, prev_datapoint_path=''):
output = []
current_datapoint = d
current_datapoint_path = prev_datapoint_path
if type(current_datapoint) is dict:
for dkey in current_datapoint:
if search_pattern in str(dkey):
c = current_datapoint_path
c+="['"+dkey+"']"
output.append(c)
c = current_datapoint_path
c+="['"+dkey+"']"
for i in search(current_datapoint[dkey], search_pattern, c):
output.append(i)
elif type(current_datapoint) is list:
for i in range(0, len(current_datapoint)):
if search_pattern in str(i):
c = current_datapoint_path
c += "[" + str(i) + "]"
output.append(i)
c = current_datapoint_path
c+="["+ str(i) +"]"
for i in search(current_datapoint[i], search_pattern, c):
output.append(i)
elif search_pattern in str(current_datapoint):
c = current_datapoint_path
output.append(c)
output = filter(None, output)
return list(output)
And you just need to use:
pprint.pprint(search(res.json(),'0.5','res.json()'))
Output:
["res.json()['claims']['P2114'][0]['mainsnak']['datavalue']['value']['amount']"]

Incorrect number of arguments executing prepared statement - Python / MySQL

When executing the following code in order to insert multiple values through a variable from python into mysql, I get:
'Incorrect number of arguments executing prepared statement ' error after executing 'result = cursor.executemany(sql_insert_query, records_to_insert)'
if i remove 'prepared=True', the error becomes:
'Not all parameters were used in the SQL statement'
import mysql.connector
connection = mysql.connector.connect(host='localhost',
database='majorprediction',
user='root',
password='')
records_to_insert = [ ('x') ,
('y'),
('z') ]
sql_insert_query = """ INSERT INTO majorpred (Major)
VALUES (%s) """
cursor = connection.cursor(prepared=True)
result = cursor.executemany(sql_insert_query, records_to_insert)
connection.commit()
Can anyone specify where is the problem?
You are passing a list of characters instead of tuples. For instance, if you try and run:
for record in records_to_insert:
print(record, isinstance(record, str), isinstance(record, tuple))
You will get:
x True False
y True False
z True False
To create tuples with a single element in python you can do the following:
records_to_insert = [
('x',),
('y',),
('z',)
]
If you have a list of parameters and want to cast all of them to tuple you can do as follows:
list_of_elements = list("list_of_characters")
tuples = [
tuple(e) for e in list_of_elements
]
Hope this helps!

Formatting data to JSON

I'm getting data from PostgreSQL using python
import psycopg2
import json
conn = psycopg2.connect(database="testdb", user="openpg", password="openpgpwd", host="127.0.0.1", port="5432")
print "Opened database successfully"
cur = conn.cursor()
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall()
print "Operation done successfully";
Then i try to format this data to JSON format: with the following command:
result = json.dumps(rows)
for row in result:
print row
but i get each caracteres of the ID, Name, address, ... in a separate row:
for example : name= abc
when execute the script i get:
a
b
c
Your result (rows) is a string (that's what json.dumps() returns). That's why when you iterate thru rows, you are iterating and printing each character.
just do print rows instead
json.dumps() returns a string, so iterating over returns each character.
To get a stream, use json.dump()

How to address a python list in MySQL query with "WHERE"? [duplicate]

This question already has answers here:
imploding a list for use in a python MySQLDB IN clause
(8 answers)
Closed 1 year ago.
I have the below code in which I want my MySQL query select those values in the 'name' column that are identical to the value in 'filenames' python list. I keep get an empty () as for the result. Can anyone help me with this?
import MySQLdb
import fnmatch
import os
import pprint
connect = MySQLdb.connect(host = "123.5.4.4", port=3366, user = "me", passwd="*********", db="data2")
filenames=[]
maindict = {}
for root, dirnames, filenames in os.walk('d:/data1'):
for filename in fnmatch.filter(filenames, '*.txt'):
filenames.append(filename)
with connect:
cur= connect.cursor()
cur.execute("SELECT version,name FROM data2.files WHERE name IN ('filenames'.join('')" )
result = cur.fetchall()
pprint.pprint(result)
You should use
cur.execute(
"SELECT version,name FROM data2.files WHERE name IN (" +
",".join(("%s",) * len(filenames)) +
")",
tuple(filenames)
)
This creates a SQL string of the form
WHERE name IN (%s,%s,%s,%s)
with as many %ss you need and makes the MySQL module replace them with the filenames data, taking care of quoting as needed.
replace:
cur.execute("SELECT version,name FROM data2.files WHERE name IN ('filenames'.join('')" )
with:
cur.execute("SELECT version,name FROM data2.files WHERE name IN ('%s')" % "','".join(filenames) )
in python shell:
>>> filenames=['file1', 'file2']
>>> query = "SELECT version,name FROM data2.files WHERE name IN ('%s')" % "','".join(filenames)
>>> print query
SELECT version,name FROM data2.files WHERE name IN ('file1','file2')
>>>

Groovy: how to use Exceptions or <If> <else> construction with MySQL queries?

I have a query from table of MySQL database, and i want check: have i got this number in table and if not - println that this number not in database, if this number exist - println that this number exist. How i can do that, using Exceptions or (If) (else) construction?
Assuming you're using an in memory hsql db:
def sql = Sql.newInstance( 'jdbc:hsqldb:mem:testDB', // JDBC Url
'sa', // Username
'', // Password
'org.hsqldb.jdbc.JDBCDriver') // Driver classname
def tim = 'tim'
def name = sql.firstRow( "SELECT name FROM users WHERE userid=$tim" )?.name
if( name ) {
println "Name was $name"
}
else {
println "Name not found"
}