python get_death_dates.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python get_death_dates.py相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

import csv
import datetime
import re
from urllib.parse import unquote
from utils import load_data, save_data
from SPARQLWrapper import SPARQLWrapper, JSON

def run():
    sparql_endpoint = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
    s = SPARQLWrapper(sparql_endpoint)

    output = csv.writer(open("death_dates.csv", "w"))
    output.writerow(['bioguide', 'name', 'death'])

    # sparql query
    query = """
      PREFIX wd: <http://www.wikidata.org/entity/>
      PREFIX wdt: <http://www.wikidata.org/prop/direct/>
      PREFIX schema: <http://schema.org/>

      SELECT ?bio ?birth ?death ?subject
      WHERE {
        ?subject wdt:P1157 ?bio .
        ?subject wdt:P569 ?birth .
        ?subject wdt:P570 ?death .
        OPTIONAL {
            ?article schema:about ?subject .
            ?article schema:inLanguage "en" .
            ?article schema:isPartOf <https://en.wikipedia.org/> .
        }
      }

    """

    # run the query and stick everything in a hash keyed by bioguide id
    s.setQuery(query)
    s.setReturnFormat(JSON)
    results = s.query().convert()
    ret = {}

    y = load_data("legislators-historical.yaml")

    deaths_by_bioguide = {}
    for row in results['results']['bindings']:
        deaths_by_bioguide[row['bio']['value']] = row['death']['value']

    for m in y:
        try:
            ddate = deaths_by_bioguide[m['id']['bioguide']]
        except KeyError:
            continue

        try:
            ddate = datetime.datetime.strptime(ddate, "%Y-%m-%dT%H:%M:%SZ").date()
        except ValueError:
            continue

        last_term_end = datetime.datetime.strptime(m['terms'][-1]['end'], '%Y-%m-%d').date()

        output.writerow([m['id']['bioguide'], m['id']['wikipedia'], str(ddate)])


if __name__ == '__main__':
  run()

以上是关于python get_death_dates.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python