couch_read.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# couch_read.py
#
# Jul/27/2020
#
# ----------------------------------------------------------------
import json
import sys
import requests
#
# ----------------------------------------------------------------
def dict_display_proc(dict_aa):
for key in sorted(dict_aa.keys()):
if ((key != '_id') and (key != '_rev')):
unit = dict_aa[key]
name = unit['name']
str_out = str(key) +"\t"+ str(name)
str_out += "\t" + str(unit['population'])
str_out += "\t" + str(unit['date_mod'])
print(str_out)
#
# ---------------------------------------------------------------
def couch_to_dict_proc (url_json):
url_all_docs = url_json + "/" + "_all_docs?include_docs=true"
rr=requests.get(url_all_docs)
list_aa = json.loads(rr.text)
dict_aa = {}
#
if ('rows' in list_aa):
for it in list_aa['rows']:
key_aa = it['key']
unit_aa = it['doc']
name = unit_aa['name']
population = unit_aa['population']
date_mod = unit_aa['date_mod']
dict_aa[key_aa] = {'name':name,'population':population,'date_mod':date_mod}
else:
sys.stderr.write("*** db not exist. ***\n")
#
return dict_aa
#
# ----------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url_json = 'http://localhost:5984/nagano'
#
dict_aa = couch_to_dict_proc(url_json)
#
dict_display_proc(dict_aa)
#
sys.stderr.write("*** 終了 ***\n")
# ----------------------------------------------------------------
実行
./couch_read.py