3
1

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 5 years have passed since last update.

Python で Domo のデータセットにデータをインポートする

Last updated at Posted at 2017-09-21

こちらで作成したデータセットにデータをインポートします。
Python で Domo のデータセットを作成する

# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	main_import_dataset.py
#
#					Sep/22/2017
#
# ------------------------------------------------------------------
import	sys
import	requests
#
from get_access_token import get_access_token_proc
from import_dataset import import_dataset_proc
from domo_config import domo_config_proc
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
config = domo_config_proc()
access_token = get_access_token_proc(config)
#
dataset_id="4e7a793f-21d1-4afb-8d87-915267f7cf8g"
#
file_csv="cities.csv"
#
json_str = import_dataset_proc(access_token,dataset_id,file_csv)
print(json_str)
#
sys.stderr.write("*** 終了 ***\n")
#
# ------------------------------------------------------------------
import_dataset.py
# -*- coding: utf-8 -*-
#
#	import_dataset.py
#
#					Sep/21/2017
#
# ------------------------------------------------------------------
import	sys
import	requests
#
from file_io import file_to_str_proc
#
# ------------------------------------------------------------------
def import_dataset_proc(access_token,dataset_id,file_csv):
	sys.stderr.write("*** import_dataset_proc *** start ***\n")
	headers = {"Authorization": "Bearer " + access_token,
			"Content-Type": "text/csv"
		}
	params = {
		"includeHeader": "true",
		}
#
	url_datasets="https://api.domo.com/v1/datasets/" + dataset_id + "/data"
#
	str_csv = file_to_str_proc(file_csv)
	str_csv = str_csv.encode('utf-8')
#
	try:
		rr=requests.put(url_datasets,params=params,data=str_csv,headers=headers)
		print(rr.status_code)
		sys.stderr.write(str(rr.status_code) + "\n")
	except Exception as ee:
		sys.stderr.write(str(ee) + "\n")
#
	sys.stderr.write("*** import_dataset_proc *** end ***\n")
#
	return rr.text
# ------------------------------------------------------------------
cities.csv
t1271,千葉,40178,2003-9-21
t1272,勝浦,40215,2003-10-15
t1273,市原,80654,2003-6-14
t1274,流山,70251,2003-9-9
t1275,八千代,42516,2003-8-4
t1276,我孫子,35987,2003-1-21
t1277,鴨川,81256,2003-7-23
t1278,銚子,29374,2003-10-26
t1279,市川,85613,2003-2-17

アクセストークンを取得するプログラムは、こちらです。
Python で Domo の アクセストークンを取得する

API の仕様はこちら
Import data into DataSet

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?