2017/10/28

CSVファイルを読み込んでlist型と辞書型に格納 - Python


  • Goal
    PythonでCSVファイルを読み込んで、List型と辞書型、集合型に格納する
  • How
    CSVモジュールのreaderメソッドを使う

    マスターファイルを作る。 テキストファイル menu.csv
     
    UDON001,500
    UDON0012,700
    SUSHI0123,1200
    TONKATSU001,2000
    RAMEN10,1000

    # -*- coding: utf-8 -*-
    import csv
    #list作成する場合
    with open('menu.csv', 'r') as f:
    csvdata = csv.reader(f)
    data = [ x for x in csvdata ]
    #集合にセットする場合
    with open('menu.csv', 'r') as f:
    csvdata = csv.reader(f)
    dataset = set(dict([ x for x in csvdata ]))
    #listを辞書型に変更
    datadic = dict(data)
    #checking
    print('List')
    print(data)
    print(len(data))
    print('Dic')
    print(datadic)
    print(len(datadic))
    print('Set')
    print(dataset)
    print(len(dataset))
    view raw CSVtoListDic.py hosted with ❤ by GitHub
  • Result
    (pytest2.7.13)~/pytest$ python CSVtoListDic.py 
    List
    [['UDON001', '500'], ['UDON0012', '700'], ['SUSHI0123', '1200'], ['TONKATSU001', '2000'], ['RAMEN10', '1000']]
    5
    Dic
    {'TONKATSU001': '2000', 'UDON001': '500', 'RAMEN10': '1000', 'UDON0012': '700', 'SUSHI0123': '1200'}
    5
    Set
    set(['TONKATSU001', 'UDON001', 'SUSHI0123', 'UDON0012', 'RAMEN10'])

    5

2017/10/22

GCP: Google App Engineにアプリーケーションをデプロイする方法 ー GAE/Py


  • 備忘メモ
    Google App Engine にデプロイするコマンド
    EC2上のUbuntu16.04からデプロイ
    APP.yamlファイルがあるディレクトリで実行すること


(pytest2.7.13) ubuntu@ip-10-0-0-253:~/pytest$ appcfg.py update --noauth_local_webserver -A "projectid" -V "version" .←Main.pyがあるディレクトリで流しているので。。。
  • 結果 --- これは何も更新されてないけど

    10:43 AM Application: (was: None); version:  (was: None)
    10:43 AM Host: appengine.google.com
    10:43 AM Starting update of app: , version: 
    10:43 AM Getting current resource limits.
    10:43 AM Scanning files on local disk.
    10:43 AM Scanned 500 files.
    10:43 AM Cloning 896 application files.
    10:43 AM Compilation starting.
    10:43 AM Compilation completed.
    10:43 AM Starting deployment.
    10:43 AM Checking if deployment succeeded.
    10:43 AM Deployment successful.
    10:43 AM Checking if updated app version is serving.
    10:43 AM Completed update of app: , version:

テキストファイルをRDBのテーブル(イメージは、品番マスターとか)風に使う - Python


  • Goal
    ChatBotが受信したテキストメッセージと、テキストファイル(CSV)をマッチングさせる。テキストファイルのままで、品番マスタの用に使って参照させる。
  • 準備
    マスターファイルを作る
    テキストファイル menu.csv
     
    UDON001,500
    UDON0012,700
    SUSHI0123,1200
    TONKATSU001,2000
    RAMEN10,1000
  • How
    1. unicodedata を使って、ひらがな・カタカナ・漢字判定と、全角・半角変換する
    2. lineでテキストファイルから1行取得して、比較を繰り返す
    3. 先頭からで一致したら抜き出す

    # -*- coding: utf-8 -*-
    import unicodedata
    inputtext = 'UDON001' # 半角全角混じりでもOK
    menu = 'menu.csv'
    # ひらがな・カタカナ・漢字があるかどうかチェック。 あったらTRUE
    def isJPN(string):
    for ch in string:
    name = unicodedata.name(ch)
    if "CJK UNIFIED" in name \
    or "HIRAGANA" in name \
    or "KATAKANA" in name:
    return True
    return False
    def main():
    if isJPN(inputtext.decode('utf-8')) is False: # 英数字のみなら
    text = unicodedata.normalize('NFKC', inputtext.decode('utf-8')) # 全角をすべて半角に変更
    with open(menu, 'r') as readf: # Menuファイルを開く
    for line in readf: # 1行づつ
    if line.find((text.upper() + ',').encode('utf-8')) == 0: # 先頭からテキストが一致して、その直後に','があったら
    itemname = line[:-1] # その行を取得
    print(itemname)
    if __name__ == '__main__':
    main()
    view raw textmatch.py hosted with ❤ by GitHub
  • 結果

    (pytest2.7.13) ubuntu@ip-10-0-0-253:~/pytest$ python testmatch.py
    UDON001,500
  • Thanks!!
    以下、参考にさせて頂きました。
    http://minus9d.hatenablog.com/entry/2015/07/16/231608