• Skip to main content
  • Skip to primary sidebar

学習記録

python3

APIが混雑している際に再取得する

2018年11月28日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def fetch_url(url):
    for x in range(30):
        try:
            f = urllib.request.urlopen(url)
        except Exception as e:
            pass
            print("トライ30回中" + str(x + 1) + "回目")
            print("1分間の停止後リトライします。")
            sleep(60)
        else:
            return f
            break
    else:
        print("全て失敗しました")

 

Filed Under: python3

jsonファイルから指定の値を探す

2018年11月28日 by 河副 太智 Leave a Comment

以下のようなjsonファイルがあった場合

 

以下のようにjsonファイルを読み込む

1
2
3
#hsコードから品名を取得する
f = open('hscodeH4.json', 'r')
jsonData = json.load(f)

jsonData[‘results’][0][“id”]
を呼び出すと’ALL’が表示される

これはjsonファイルの’results’から見て0番目の辞書の中の”id”の
文字列を出力するからであり、

jsonData[‘results’][1][“id”]
を呼び出すと同じ原理で’TOTAL’が表示される

同じ要領で
jsonData[‘results’][0][“text”]
を呼び出すと’results’から見て0番目の辞書の中の”text”の
文字列を出力するので’All HS2012 categories’が表示される

 

jsonファイルの中のどこかの”text”が欲しい場合は
その”text”が存在する位置を探す作業が必要になる。

例えばid が”121299″の辞書にある”text”が欲しい場合、
以下のようにforを使用してデータを一つづつ見て行く。

そして[‘results’][i][“id”]と予め知っていたidが一致した時の
forで設定されているiの値が欲しい”text”を含む位置になるので
jsonData[‘results’][i][“text”]とすれば目的の”text”が手に入る

例

1
2
3
for i in range(len(jsonData["results"])):
    if jsonData['results'][i]["id"] == 121299:        
        itemname = jsonData['results'][i]["text"]

 

Filed Under: python3

PythonでxserverにFTPアップロード

2018年11月8日 by 河副 太智 Leave a Comment

pythonファイルと同じディレクトリにあるhtmlファイルをすべて
ftpアップロードするコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import glob
import string
import os
from ftplib import FTP
import urllib.request
 
filelist=glob.glob('*.html')
 
ftp=FTP('sv2142.xserver.jp')
ftp.set_pasv("true")
ftp.login('ここにID','ここにパスワード')
ftp.cwd('/aaaa.com/public_html/bbb/') #ホスト側のディレクトリ
 
 
 
for file in filelist:
#     f=open(file,'rb')
    with open(file, "rb") as f:
        filename=os.path.basename(file)
        cmd='STOR '+filename
 
        ftp.storbinary(cmd,f)
        f.close()
ftp.quit()
 
for file in filelist:
    os.remove(file)

 

Filed Under: python3

HTMLファイルを生成

2018年10月17日 by 河副 太智 Leave a Comment

現時点ではhtmlファイルをゼロから生成する方法が不明

以下は指定のhtmlファイルを開いて別名で保存する方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# write-html-2-mac.py
import webbrowser
 
f = open('helloworld.html','w')
 
message = """<html>
<head></head>
<body><p>Hello World!</p></body>
</html>"""
 
f.write(message)
f.close()
 
#Change path to reflect file location
filename = 'file:///Users/username/Desktop/programming-historian/' + 'helloworld.html'
webbrowser.open_new_tab(filename)

 

Filed Under: python3

jsonファイルから情報を取得

2018年10月3日 by 河副 太智 Leave a Comment

Comtradeの国名一覧jsonファイルから国名を取得します。

1
2
url = urllib.request.urlopen("https://comtrade.un.org/data/cache/partnerAreas.json")
jdata = json.loads(url.read().decode())

jsonファイルのデータを個別に指定して変数に代入

1
reporting_area = "&r=" + (jdata['results'][i]["id"])

i はfor文のiです。

Filed Under: python3

Jupyternotebook初期設定

2018年9月17日 by 河副 太智 Leave a Comment

c:ドライブのユーザーフォルダー(自分の名前やメールアドレスの一部)に
新規フォルダを作る(jupyter_notebook等)

次にコマンドプロンプトから

>cd jupyter_notebook

>jupyter notebook

と入力する

Filed Under: python3 Tagged With: Jupyter, 環境, 環境構築

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Page 5
  • Interim pages omitted …
  • Page 12
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in