-1

This is my code below, I want to send python variable value in place of dbpedia:Imran_Khan. please provide me proper solution of this query Code:

 from SPARQLWrapper import SPARQLWrapper, JSON
inputvar=input("enter name:")
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dct:    <http://purl.org/dc/terms/>

SELECT DISTINCT ?name  ?birthDate  ?abs ?c ?des  ?r ?bn  ?pro WHERE { 
  dbpedia:Imran_Khan # **(here I want to pass the variable in which user input is stored)** 
                     dbo:birthDate ?birthDate ;
                     dbo:abstract ?abs 
                    FILTER (lang(?abs) = 'en')

}
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print(result["abs"]["value"])
    print(result["birthDate"]["value"])
TallTed
  • 9,069
  • 2
  • 22
  • 37
  • 1
    The same question was asked [yesterday](https://stackoverflow.com/questions/54827065/how-to-create-a-parameterised-query-in-sparqlwrapper-in-python)...please use the search before opening a question. And I assume it's a student project so please try to work together and share knowledge. – UninformedUser Feb 23 '19 at 10:31
  • 3
    Possible duplicate of [How to create a parameterised query in SPARQLWrapper in python](https://stackoverflow.com/questions/54827065/how-to-create-a-parameterised-query-in-sparqlwrapper-in-python) – UninformedUser Feb 23 '19 at 10:32
  • Thanks for seeing my problem, I have searched for my problem before asking questions and I didn't find any answers related to my query and the link you provide it's not related to my query my problem is different – Mohsin Rashid Feb 23 '19 at 12:21
  • 2
    why is this different? you just want to pass a Python variable into a Python string. This has nothing to do with SPARQL at all, it's pure Python and in the link I posted shown how to do it. – UninformedUser Feb 23 '19 at 13:14
  • 1
    In addition, if a user enters a string, how to you ensure that this string will map the name of a person? right now, it looks like you do `dbpedia:USER_INPUT` or at least that's what you're trying once you solved the trivial Python issue. – UninformedUser Feb 23 '19 at 13:16

1 Answers1

0

You should use this code, I am sure it will solve your problem and itis easy to understand.

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF
print('Enter your name:')
x = input()
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("\n"
                    "    PREFIX dbpedia: <http://dbpedia.org/resource/>"
                    " PREFIX dbo: <http://dbpedia.org/ontology/>"
                    "PREFIX dbp: <http://dbpedia.org/property/>\n"
                    "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
                    "PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>\n"
                    "PREFIX dct:    <http://purl.org/dc/terms/>\n"
                    "SELECT DISTINCT ?name  ?birthDate   WHERE {\n"
                   "dbpedia:"+x+" foaf:name ?name\n"
                    "}\n"
                    "                   ")
print('\n\n*** JSON Example')
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    a = result["name"]["value"]
print(a)
Saad Khan
  • 31
  • 6