0

I have a list of names (hundreds of them) that are already transformed to Q-numbers in wikidata using python. For each Q-number (person) I want to get some basic information such as place_of_birth, nationality, etc.

SELECT DISTINCT ?name ?nameLabel ?genderLabel ?placeofbirth ?nationality (year(?birthdate) as ?birthyear) (year(?deathdate) as ?deathyear)
WHERE
{
  ?name wdt:P106/wdt:P279* wd:Q1028181  # painter
  FILTER (?name IN (wd:Q2674488)) # James Seymour 
  OPTIONAL { ?name wdt:P569 ?birthdate. }        
  OPTIONAL { ?name wdt:P27 ?nationality. }
  OPTIONAL { ?name wdt:P21 ?gender. }
  OPTIONAL { ?name wdt:P19 ?placeofbirth. }
  OPTIONAL { ?name wdt:P570 ?deathyear. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Using SPARQL, I can search two or three people at a time by adding Q-numbers into "FILTER", but how can I loop through all Q-numbers in a python list? Thanks a lot!

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • It's not really clear to me where you're stuck. You already know how to use `FILTER...IN`, and you know that the argument of `IN` is a list. If you have a bigger list of identifiers, just create a bigger list as the argument of the `IN` operator. Alternatively, the `VALUES` clause might be something to look into. – Jeen Broekstra May 27 '19 at 23:21
  • @Jeen Broekstra Thanks! The python list has hundreds of Q-numbers, also I don't know how to send the list to SPARQL without coping and pasting (because of the size of the list). – June_Stephanie May 28 '19 at 00:32
  • 1
    Possible duplicate of [Search Multiple Queries from List in SPARQL](https://stackoverflow.com/questions/36796644/search-multiple-queries-from-list-in-sparql) – ivan_pozdeev May 28 '19 at 00:45
  • 1
    I guess it remains unclear where you're stuck. I mean you just have to generate the query string and send it to the SPARQL endpoint. Generating a Python string is trivial, isn't it? `list.join(", ")` - and this combined with the rest of the query string sent to Python. Please clarify where exactly you're stuck, i.e. show the Python – UninformedUser May 28 '19 at 03:18

0 Answers0