• Skip to main content
  • Skip to primary sidebar

学習記録

python3

zipファイル(圧縮ファイル)を解凍

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

Pythonでzipファイルを解凍する方法
os内の指定フォルダにダウンロードした複数のzipファイルを対象にして
指定のフォルダに展開した。

1
2
3
4
5
6
7
8
9
10
import glob
import os
import zipfile
 
download_zip=glob.glob(r"C:\Users\AAAAA\Downloads/*.zip")
print("対象ファイルは:"+download_zip[0])
 
for i in range (len(download_zip)):
    with zipfile.ZipFile(download_zip[i]) as existing_zip:
        existing_zip.extractall(r"C:\Users\AAAAA\Downloads/")

 

Filed Under: python3 Tagged With: zip, zipファイル, 圧縮, 解凍

日本語の混じったcsvファイル読み込みエラー

2019年2月25日 by 河副 太智 Leave a Comment

日本語の混じったcsvファイルをデータフレームに読み込もうとすると
以下のようなエラーが出るdd

‘utf-8’ codec can’t decode byte 0x83 in position 0: invalid start byte

そのような時は以下のような方法で読み込めば
問題なく処理される

1
2
3
4
import codecs
# df = pd.read_csv("data/201902251104359122801.csv")
with codecs.open("data/201902251104359122801.csv", "r", "Shift-JIS", "ignore") as file:
    df = pd.read_table(file, delimiter=",")

 

Filed Under: python3

タイムアウトの設定

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

エラー文が表示されずにプログラムが停止する場合に処理を再開したい
場合は一定時間の経過をエラーとみなすためにタイムアウトの設定が
必要になる。

Pythonの場合は以下のように記述する

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time
import timeout_decorator
 
 
def very_long_function():
    for i in range(100):
        print i
        time.sleep(1)
 
 
@timeout_decorator.timeout(5)
def test():
    very_long_function()
 
 
if __name__ == '__main__':
    try:
        test()
    except:
        print "test timed out :("
    else:
        print "test finished successfully :)"

 

Filed Under: python3

変数のメモリ内容を一覧表示

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

各変数のメモリ内容を一覧表示する
オーバーフローを防ぐ時に使える

■jupyterにて使用可能

1
2
3
4
5
6
7
8
    #メモリ確認
    import sys
 
    print("{}{: >25}{}{: >10}{}".format('|','Variable Name','|','Memory','|'))
    print(" ------------------------------------ ")
    for var_name in dir():
        if not var_name.startswith("_"):
            print("{}{: >25}{}{: >10}{}".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))

以下のように表示される

 

| Variable Name| Memory|
————————————
| Client| 1304|
| GetPosts| 1016|
| GetUserInfo| 1016|
| In| 96|
| NewPost| 1016|
| Out| 288|
| WordPressPost| 1304|
| a| 32|
| amount_country| 28|
| append_times| 28|
| append_timesi| 28|
| bunsho| 136|
| classification| 55|
| com| 55|
| data| 768|
| data2| 264|
| data3| 264|
| datai| 912|
| date| 400|
| datetime| 400|
| df| 94318|
| df2| 1008|
| df3| 50079|
| df4| 1025|
| df_hsname| 3006|
| df_s| 74071|
| dfi| 92814|
| dfi2| 86330|
| dfi3| 54441|
| dfi4| 1011|
| exit| 56|
| f| 216|
| fetch_url| 136|
| fig| 56|
| fmt| 57|
| fullname| 228|
| get_data| 136|
| get_ipython| 64|
| go| 80|
| i| 28|
| itemname| 71|
| itemname2| 228|
| jdata| 288|
| json| 80|
| jsonData| 288|
| kiji_all| 54100|
| layoute1| 56|
| layoute2| 56|
| layouti1| 56|
| layouti2| 56|
| make_url| 136|
| math| 80|
| now| 48|
| partner| 768|
| partner2| 264|
| partner2i| 264|
| partner_area| 53|
| partneri| 912|
| pd| 80|
| pio| 80|
| pkge| 264|
| pkgi| 264|
| post| 56|
| pyo| 80|
| quit| 56|
| relativedelta| 1016|
| sleep| 72|
| ssl| 80|
| sys| 80|
| timedelta| 400|
| title| 105|
| trace| 56|
| trace2| 56|
| trace3| 56|
| upftp| 80|
| url| 56|
| urllib| 80|
| valuee| 768|
| valuei| 912|
| var| 28|
| var_name| 57|
| wp| 56|
| x| 28|

Filed Under: Jupyter, python3

文字列の改行(変数内)

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

ジュピターではスラッシュが¥で表示される
¥の直後にスペースがあるとエラーになる

1
2
3
4
5
6
7
8
9
10
11
exprice = "<h1 id=\"rittai\">Export Trade Statistics of \""+ itemname + "\"(HScode:" + com +")" + "</h1><br><br>" + class_tip + \
    "The graph below shows export statistics of<br><b>" + itemname + "</b>(HScode:" + com + ") worldwide in " + str(df3["Year"][0]) + class_end + \
    tag_st + up_url + com +"expvalue"+ ".html\"" + end_value+class_tip+ \
    "The statistic shows a ranking of the top export countries worldwide in " + str(df3["Year"][0])+ "<br>" +\
    partner[0] + " ranked first in exports of \"" + itemname +"\"" + " Total export value " +  "{:,}".format(valuee[0]) + "USD <br>"  + \
    partner[0] + " is followed by " + \
    partner[1] + ": " +  "{:,}".format(valuee[1]) + "USD <br>" +\
    partner[2] + ": " +  "{:,}".format(valuee[2]) + "USD <br>" +\
    partner[3] + ": " +  "{:,}".format(valuee[3]) + "USD <br>" +\
    partner[4] + ": " +  "{:,}".format(valuee[4]) + "USD and <br>" +\
    partner[5] + ": " +  "{:,}".format(valuee[5]) + "USD <br>" + "<p>"+class_end

 

Filed Under: python3

リストにforで追加していく

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

1
2
3
4
#国名リスト化
partner = []
for var in df_s["Reporter"]:
    partner.append(var)

 

Filed Under: python3

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • 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