*disclaimer
495816
Python
- Python
Pythonの始め方
Pythonのサンプル
Python.tips
Tips
- パスを通しておく
- Windows の場合は、環境変数「TEMP」の「PATH」に、追加する
- 改行コードが問題になる場合があるので注意
Links
- PythonRecipe
- 処理の基本パターンが一覧になっていて便利
実行ファイルにする
- 文字コードは UTF-8にしておくこと
pyinstaller
https://github.com/pyinstaller/pyinstaller/releases
- Python 3.7に対応
コマンドプロンプトで実行
pip install pyinstaller
- パイソンのスクリプトを書いておいて、
pyinstaller sample.py --onefile --noconsole
とすると、「dist」というフォルダーができて、その中に、sample.exeファイルができる。
これで実行可能。
- --onefile オプションで、必要なファイルを一つのファイルにまとめてコンパイル
- --noconsole オプションで、実行時にPythonのコンソールを出さない
Windows
https://www.python.org/downloads/release/python-373/
Windows x86-64 executable installer
GUI
Tkinter
https://wiki.python.org/moin/TkInter
http://www.nct9.ne.jp/m_hiroi/light/py3tk01.html
Anaconda
Jupyter Notebook
RStudioで、Pythonを使う
環境設定
手順
- 「File」>「New File」>「Python Script」
実行
- スクリプトを書いて、実行部分を選んで、Run
ライブラリーが必要な場合
- 左下のペインで「Terminal」を選んで、そこで、pip install ライブラリ を実行しておく。
$ pip install numpy
Reference
https://qiita.com/otsukimi200624/items/c707f87b95dcde2d8354
CGI
文字コードをUTF-8に設定
/etc/httpd/conf/httpd.conf
<Directory "/var/www/cgi-bin"> # added below for Python SetEnv PYTHONIOENCODING utf-8
スクリプトの書き出し
#!/usr/bin/python3 # ヘッダー情報、テキストファイルを指定 print("Content-Type: text/plain; charset=utf-8\n")
Pythonの中からシェルスクリプトを呼び出す
- subprocessモジュール
import subprocess subprocess.call(["ls","-l"])
- コマンドラインは、半角スペース単位で、項目を別の要素として書いておく。
- References
https://dev.classmethod.jp/articles/python-subprocess-shell-command/
- SELinuxの設定
https://qiita.com/100/items/f7f43994568836b4a453
録音
- PyAudioを使う
References
https://watlab-blog.com/2019/06/20/pyaudio-record/
https://qiita.com/mix_dvd/items/adce7636e2ab33b25208
https://tips-memo.com/python-recording
https://algorithm.joho.info/programming/python/pyaudio-record/
リストとタプル
- 配列のようなもの
[値, 値, 値] としたらリスト 角カッコ (値, 値, 値) としたらタプル 丸カッコ
- いずれもインデクス番号(0から)で、要素を指定できる。
score = (170, 60, 26) shin = score[0] tai = score[1] nen = score[2]
print("身長は", shin, "センチで体重は", tai, "キロです")
身長は 170 センチで体重は 60 キロです
- リストは要素をインデックスで指定して入れ替えられる
- タプルはできない
Reference
https://www.python.jp/train/tuple/index.html
参考書
https://github.com/RyokoKuga/python-app-book
Python環境構築ガイド
https://www.python.jp/install/install.html
Python Notes for Linguistics
https://alvinntnu.github.io/python-notes/intro.html
A Quick Guide to Part of speech Tagging
https://www.dataknowsall.com/pos.html
便利なスクリプトLinks
- Pythonによる自動化プログラムの事例
https://gammasoft.jp/python-example/
- Python の基本
https://python.keicode.com/lang/
https://sugiura-ken.org/wiki/