I would like to do a Wikidata query of many values that are listed in a column of a CSV file on my computer.
How can I load the values from the CSV file into the Wikidata query automatically without copying them in manually?
So far I have worked with the Wikidata query in Visual Studio Code.
This is the query I made for one person:
SELECT ?Author ?AuthorLabel ?VIAF ?birthLocation
WHERE {
VALUES ?VIAF {"2467372"}
?Author wdt:P214 ?VIAF ;
wdt:P19 ?birthLocation .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_Language],de". }
}
I want to automatically load many values into the curly brackets of the query above from the column of my CSV file.
2
Answers
So, say you have a file
my_file.csv
with the following content:First of all, import a python library for reading files (like
fileinput
).Then declare the pattern that you want to use for your query, using
%s
as placeholder for the identifiers.Now, build a list of identifiers as follows:
And finally join the list using a space character as separator and pass this string to your query pattern:
This is the final code:
Executing it, you’ll get:
First, I feel compelled to point out that if you don’t already know a programming language OpenRefine can do this for you in a few clicks.
Having said that, here’s a basic Python program that accomplishes what you literally asked for – reading a set of VIAF ids and adding them to your query:
It expects a CSV file with a column called
viaf
and will ignore all other columns. e.g.I’ve tweaked the query slightly to:
This makes the assumption that you’ve got a small enough set of identifiers to be able to use a single query, but you can extended it to: