1

How to get the results related to dates from Sparql queries in wikidata in JSON through Python codes in the same format they appear when I query directly through the web?

When I query in Wikidata Query Portal (Try it), some dates appear formatted like 21 de junho de 1839, but when i request them via Python with the library SPARQLWrapper, they appear this way: 1839-06-21T00:00:00Z. How to get the "beautified version" from site in JSON?

  • 1
    same question was asked some weeks ago: https://stackoverflow.com/questions/71556196/format-dates-based-on-date-precision - short answer: what you see in the browser is just some post-processing in the client code, the raw data, and that is what SPARQL returns, is of course the datetime literal - this allows any client to use their own rendering. Indeed, you can try to create your own rendered string directly in SPARQL, but I don't see why. Once you "show" the data somewhere, it's easier to use one of the APIs then to format date literals – UninformedUser Apr 10 '22 at 03:33
  • So is there any way to have the same browser result in json? Unfortunately I don't know how to do it. I'm starting to study SPARQL queries. Thanks – Fabricio Silva Apr 10 '22 at 04:07
  • 1
    SPARQL allows to get year, month and day from a datetime literal. With BIND keyword and CONCAT you can then build your own string. Indeed, for each month number you need the corresponding month string, use VALUES keywod for this – UninformedUser Apr 10 '22 at 10:11
  • What is beautiful is in the eyes of the spectator. For me the ISO version is a beauty compared to the localized version. It is much easier to go from ISO to localized then the other way round. – Wolfgang Fahl Jul 22 '22 at 14:25

1 Answers1

1

I have slightly modified your query and added it to the wikidata.yaml sample queries of the pyLodStorage library for which i am committer so that

sparqlquery -qp wikidata.yaml -qn MachadoDeAssis -f github

works and gives the cut&paste result shown further down below.

sparqlquery -qp wikidata.yaml -qn MachadoDeAssis -f json 

returns

[
  {
    "author": "http://www.wikidata.org/entity/Q311145",
    "authorLabel": "Machado de Assis",
    "birthDate": "1839-06-21 00:00:00"
  }
]

now you can pipe the result thru

| jq '.[] | .birthDate | strptime("%Y-%m-%d %H:%M:%S") | strftime("%c")'
sparqlquery -qp wikidata.yaml -qn MachadoDeAssis -f json | jq '.[] | .birthDate | strptime("%Y-%m-%d %H:%M:%S") | strftime("%c")'

which results in

"Fri Jun 21 00:00:00 1839"

if all runs well in a different locale the output should be according to your locale.

MachadoDeAssis

query

# modified by WF 2022-07-22
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?author ?authorLabel ?birthDate
WHERE 
{
  VALUES ?authorLabel {
    "Machado de Assis"@pt
  }
  # Instance of Human
  ?author wdt:P31 wd:Q5 .
  # Joaquim Maria Machado de Assis
  ?author rdfs:label ?authorLabel.
  FILTER (LANG(?authorLabel) = "pt").
  # birthDate
  ?author wdt:P569 ?birthDate .
}

try it!

result

author authorLabel birthDate
http://www.wikidata.org/entity/Q311145 Machado de Assis 1839-06-21 00:00:00
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186