0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3: CouchDB のデータを読む (Read)

Posted at
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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?