使用python的SPARQL请求有效,但一旦扩展了请求的时间范围,则会导致JSONDecodeError

问题描述

在尝试从python中的Wikidata请求数据时,我遇到了问题,非常感谢您的帮助。

虽然通常可以正常工作(以CODE SNIPPET 1进行说明),但是如果对查询稍加修改,则运行代码会导致JSONDecodeError错误,从而覆盖更长的时间(如CODE SNIPPET 2所示)...

代码片段1:

query = """
SELECT ?person ?personLabel ?dob ?place_of_birth ?place_of_birthLabel ?date_of_death ?place_of_death ?place_of_deathLabel ?political_party ?political_partyLabel ?sex_or_gender ?sex_or_genderLabel ?Wikimedia_import_URL ?occupation ?occupationLabel ?work_location ?work_locationLabel ?educated_at ?educated_atLabel ?imported_from_Wikimedia_project ?imported_from_Wikimedia_projectLabel ?source_website_for_the_property ?stated_in ?stated_inLabel ?religion ?religionLabel ?VIAF_ID ?ISNI ?Deutsche_Biographie_ID ?DBS_ID ?place_of_detention ?place_of_detentionLabel ?country_of_citizenship ?country_of_citizenshipLabel ?member_of_military_unit ?member_of_military_unitLabel ?conflict ?conflictLabel ?military_rank ?military_rankLabel ?military_branch ?military_branchLabel ?participant_in ?participant_inLabel ?award_received ?award_receivedLabel ?described_by_source ?described_by_sourceLabel ?academic_degree ?academic_degreeLabel ?field_of_work ?field_of_workLabel ?noble_title ?noble_titleLabel WHERE {
  ?person wdt:P31 wd:Q5;
    wdt:P569 ?dob.
  FILTER(("1870-01-02"^^xsd:dateTime <= ?dob) && (?dob <= "1870-01-04"^^xsd:dateTime))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
  OPTIONAL { ?person wdt:P19 ?place_of_birth. }
  OPTIONAL { ?person wdt:P570 ?date_of_death. }
  OPTIONAL { ?person wdt:P20 ?place_of_death. }
  OPTIONAL { ?person wdt:P102 ?political_party. }
  OPTIONAL { ?person wdt:P21 ?sex_or_gender. }
  OPTIONAL { ?person wdt:P4656 ?Wikimedia_import_URL. }
  OPTIONAL { ?person wdt:P106 ?occupation. }
  OPTIONAL { ?person wdt:P937 ?work_location. }
  OPTIONAL { ?person wdt:P69 ?educated_at. }
  OPTIONAL { ?person wdt:P143 ?imported_from_Wikimedia_project. }
  OPTIONAL { ?person wdt:P1896 ?source_website_for_the_property. }
  OPTIONAL { ?person wdt:P248 ?stated_in. }
  OPTIONAL { ?person wdt:P140 ?religion. }
  OPTIONAL { ?person wdt:P214 ?VIAF_ID. }
  OPTIONAL { ?person wdt:P213 ?ISNI. }
  OPTIONAL { ?person wdt:P7902 ?Deutsche_Biographie_ID. }
  OPTIONAL { ?person wdt:P4007 ?DBS_ID. }
  OPTIONAL { ?person wdt:P5019 ?occupation. }
  OPTIONAL { ?person wdt:P2632 ?place_of_detention. }
  OPTIONAL { ?person wdt:P27 ?country_of_citizenship. }
  OPTIONAL { ?person wdt:P7779 ?member_of_military_unit. }
  OPTIONAL { ?person wdt:P607 ?conflict. }
  OPTIONAL { ?person wdt:P410 ?military_rank. }
  OPTIONAL { ?person wdt:P241 ?military_branch. }
  OPTIONAL { ?person wdt:P1344 ?participant_in. }
  OPTIONAL { ?person wdt:P166 ?award_received. }
  OPTIONAL { ?person wdt:P1343 ?described_by_source. }
  OPTIONAL { ?person wdt:P512 ?academic_degree. }
  OPTIONAL { ?person wdt:P101 ?field_of_work. }
  OPTIONAL { ?person wdt:P97 ?noble_title. }
}
"""

import requests

url = 'https://query.wikidata.org/sparql'
r = requests.get(url,params={'format': 'json','query': query})

json_format = r.json()

如果来自CODE SNIPPET 1的这一行发生错误

FILTER(("1870-01-02"^^xsd:dateTime <= ?dob) && (?dob <= "1870-01-04"^^xsd:dateTime))

已替换为以下行(代码片段2):

FILTER(("1870-01-02"^^xsd:dateTime <= ?dob) && (?dob <= "1874-01-04"^^xsd:dateTime))

替换引起的错误如下:

json.decoder.JSONDecodeError: Invalid control character at: line 2345752 column 44 (char 64968579)

浏览互联网寻求帮助后,我在该行中添加了“ strict = False”,该行负责解码(以消除由于在检索到的数据中包含“ \ n”之类的模式而引起的问题),导致代码片段3:

import requests

url = 'https://query.wikidata.org/sparql'
r = requests.get(url,'query': query})

json_format = r.json(strict=False)

但是,这引发了另一种解码错误

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2031964 column 9 (char 56246827)

因此,我再次修改代码,试图避免从请求库(代码片段4)中使用.json()函数

import requests

url = 'https://query.wikidata.org/sparql'
r = requests.get(url,'query': query})

json_str = r.text

import json
json_format = json.loads(json_str,strict=False)

导致第三种解码错误

json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1521988 column 12 (char 42127071)

我还尝试在设置了'strict = True'的同时,从最后一个代码段中定义的变量json_str手动将模式替换为'\ n'。不幸的是,我的方法都没有用。 好吧,现在就这样。如果有人对如何解决此问题有任何想法,我将不胜感激:)

干杯, 马吕斯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...