it技術:python
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| it技術:python [2022/04/06 10:24] – yajuadmin | it技術:python [2022/04/22 10:31] (現在) – yajuadmin | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Python ====== | ====== Python ====== | ||
| C# | C# | ||
| - | Visual Studio 2022を使用\\ | + | Visual Studio 2022を使用するも、途中で拡張機能が充実しているVisual Studio Codeに変更した。 |
| - | + | ===== Visual Studio関連 | |
| - | ===== Tips ===== | + | ==== Visual Studio上でUTF-8にして保存したい |
| - | ==== Visual Studio上でUTF-8にして保存したい ==== | + | |
| Visual StudioでPythonファイルを保存するとShift-JISになりデバッグエラーになってしまう。\\ | Visual StudioでPythonファイルを保存するとShift-JISになりデバッグエラーになってしまう。\\ | ||
| UTF-8で保存できるようにしたい。 | UTF-8で保存できるようにしたい。 | ||
| 行 26: | 行 25: | ||
| [[https:// | [[https:// | ||
| + | |||
| + | ===== Visual Studio Code関連 ===== | ||
| + | ==== 環境構築 ==== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | ==== 拡張機能 ==== | ||
| + | [[https:// | ||
| + | |||
| + | 下記の拡張機能をインストール | ||
| + | |||
| + | * indent-rainbow | ||
| + | * ZenKaku | ||
| + | * Trailing Spaces | ||
| + | * Python Indent | ||
| + | * Python Docstring Generator | ||
| + | |||
| + | ===== コーディング関連 ===== | ||
| + | ==== コーディング規約 ==== | ||
| + | PEP(Python Enhancement Proposal)の中で、PEP 8は「Style Guide for Python Code」と銘打ち、Pythonのコードを記述する際の基本的なコーディングスタイルを定めている。 | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| ==== main関数の書き方 ==== | ==== main関数の書き方 ==== | ||
| 行 43: | 行 65: | ||
| [[https:// | [[https:// | ||
| - | ==== コーディング規約 | + | ==== fromやimportの基礎 |
| - | PEP(Python Enhancement Proposal)の中で、PEP 8は「Style Guide for Python Code」と銘打ち、Pythonのコードを記述する際の基本的なコーディングスタイルを定めている。 | + | [[https://techacademy.jp/magazine/23818|Pythonのfromやimportの基礎を現役エンジニアが解説【初心者向け】]] |
| - | + | ==== コメント記載方法 | |
| - | * [[https://qiita.com/ | + | |
| - | * [[https:// | + | |
| - | + | ||
| - | === コメント記載方法 === | + | |
| Google Styleを採用 | Google Styleを採用 | ||
| 行 69: | 行 87: | ||
| </ | </ | ||
| - | === 定義変数 === | + | ==== 定義変数 |
| <wrap em> | <wrap em> | ||
| </ | </ | ||
| 行 93: | 行 111: | ||
| </ | </ | ||
| - | ==== 自作クラスで' | + | ==== 文字列の囲み |
| - | 原因はimportの書き方が間違っているから\\ | + | Python では、単一引用符 ' で囲まれた文字列と、二重引用符 " で囲まれた文字列は同じです。\\ |
| - | 以下の書き方だとエラーになる。 | + | PEP 8では、どちらを推奨するかの立場は示しません。 |
| + | |||
| + | C# | ||
| + | |||
| + | === 文字列の長さ取得 === | ||
| + | C# | ||
| <code python> | <code python> | ||
| - | import myClass | + | ✕ " |
| - | myInstance = myClass() | + | |
| - | </ | + | |
| - | 下の書き方に修正すると動く。 | + | ◯ len(" |
| - | <code python> | + | |
| - | from (myClassのファイル名) import myClass | + | |
| - | myInstance = myClass() | + | |
| </ | </ | ||
| - | [[http://mynetabook.blogspot.com/2018/10/importtypeerror-module-object-is-not.html|[トラブル] 自作クラスのimport時にTypeError: | + | [[https://methane.hatenablog.jp/entry/20090702/1246556675|len が関数になっている理由]] |
| - | ==== クラス内のメソッドを別のメソッド内で呼ぶ | + | ==== switch case文はない |
| - | クラス内のメソッドを別のメソッド内で呼ぶと「name ' | + | Pythonにswitch文が実装されない理由として、公式ドキュメントには以下のように記述されています。 |
| - | 関数の順序は関係ない、クラス内のメソッドの第1引数は必ずselfを付け、self.xxx で呼び出す必要があった。 | + | >if… elif… elif… else の繰り返しで簡単に同じことができます。switch 文の構文に関する提案がいくつかありましたが、範囲判定をするべきか、あるいはどのようにするべきかについての合意は (まだ) 得られていません。 |
| + | [[https:// | ||
| - | Pythonではメソッドの第一引数は「self」が必須であり、自身のクラスのインスタンスを受け取るとコーディング規約で決まっているらしい。\\ | + | If文を駆使することで同様の処理が行えるため、議論こそあったようですが実装はされなかったようです。\\ |
| - | > | + | しかし、Python3.10からはswitch文と同様の構文の「match文」が導入されます。 |
| - | https:// | + | |
| - | * [[https://teratail.com/ | + | [[https://camp.trainocate.co.jp/magazine/python-switch-case/|Pythonでswitch/case文がない理由は?代用方法を解説!]] |
| - | * [[https:// | + | |
| - | * [[https:// | + | |
| - | * [[https:// | + | |
| - | + | ||
| - | ==== コマンドライン引数の処理 ==== | + | |
| - | [[https:// | + | |
| - | + | ||
| - | ==== ファイル出力 ==== | + | |
| - | [[https:// | + | |
| - | + | ||
| - | === フォルダ未存在時作成 === | + | |
| - | フォルダ名は、フルパスで指定すればいい。 | + | |
| + | ==== フォーマット書式 ==== | ||
| <code python> | <code python> | ||
| - | import os | ||
| - | |||
| - | new_path = " | ||
| - | if not os.path.exists(new_path): | ||
| - | os.mkdir(new_path) | ||
| - | </ | ||
| - | |||
| - | [[https:// | ||
| - | ==== フォーマット書式 ==== | ||
| - | <code pyhon> | ||
| line = " | line = " | ||
| print(line) | print(line) | ||
| 行 149: | 行 146: | ||
| 単純に左から順に一対一になっている場合は、以下のようにインデックス番号を省略できます。 | 単純に左から順に一対一になっている場合は、以下のようにインデックス番号を省略できます。 | ||
| - | < | + | < |
| line = " | line = " | ||
| print(line) | print(line) | ||
| 行 184: | 行 181: | ||
| %fはマイクロ秒なのでミリ秒にする。 | %fはマイクロ秒なのでミリ秒にする。 | ||
| - | < | + | < |
| from datetime import datetime | from datetime import datetime | ||
| 行 193: | 行 190: | ||
| [[https:// | [[https:// | ||
| + | ==== 例外処理 ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | === 例外を発生させる === | ||
| + | 自作の例外を発生させるraiseの使い方 | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | === catchした例外をそのまま親まで届ける === | ||
| + | [[https:// | ||
| + | |||
| + | 式を伴わなければ、 raise は現在のスコープで最終的に有効になっている例外を再送出します。 | ||
| + | |||
| + | <code python> | ||
| + | try: | ||
| + | ... | ||
| + | except Exception as e: | ||
| + | raise | ||
| + | そもそもraiseには例外インスタンスが渡せるので | ||
| + | </ | ||
| + | <code python> | ||
| + | try: | ||
| + | ... | ||
| + | except Exception as e: | ||
| + | raise e # ★ | ||
| + | でもいいです。こちらだとTracebackに★の行の情報が加えられます。 | ||
| + | except節の中でifの分岐などでraiseが2箇所以上現れるならこちらで。 | ||
| + | </ | ||
| + | |||
| + | ==== 配列およびリスト処理 ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ==== クラス内のメソッドを別のメソッド内で呼ぶ ==== | ||
| + | クラス内のメソッドを別のメソッド内で呼ぶと「name ' | ||
| + | 関数の順序は関係ない、クラス内のメソッドの第1引数は必ずselfを付け、self.xxx で呼び出す必要があった。 | ||
| + | |||
| + | Pythonではメソッドの第一引数は「self」が必須であり、自身のクラスのインスタンスを受け取るとコーディング規約で決まっているらしい。\\ | ||
| + | > | ||
| + | > | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | ===== 自作クラスで' | ||
| + | 原因はimportの書き方が間違っているから\\ | ||
| + | 以下の書き方だとエラーになる。 | ||
| + | |||
| + | <code python> | ||
| + | import myClass | ||
| + | myInstance = myClass() | ||
| + | </ | ||
| + | |||
| + | 下の書き方に修正すると動く。 | ||
| + | <code python> | ||
| + | from (myClassのファイル名) import myClass | ||
| + | myInstance = myClass() | ||
| + | </ | ||
| + | |||
| + | [[http:// | ||
| + | |||
| + | |||
| + | ===== コマンドライン引数の処理 ===== | ||
| + | [[https:// | ||
| + | |||
| + | ===== ファイル関連 ===== | ||
| + | ==== パス操作 ==== | ||
| + | <code python> | ||
| + | import os | ||
| + | |||
| + | filepath = r" | ||
| + | |||
| + | # ファイル名 os.path.basename() | ||
| + | print(os.path.basename(filepath)) | ||
| + | |||
| + | # フォルダ名 os.path.dirname() | ||
| + | print(os.path.dirname(filepath)) | ||
| + | |||
| + | # 拡張子の取得 os.path.splitext() | ||
| + | print(os.path.splitext(filepath)[1]) | ||
| + | |||
| + | # 拡張子なしのパス os.path.splitext() | ||
| + | print(os.path.splitext(filepath)[0]) | ||
| + | |||
| + | # 拡張子なしのファイル名 os.path.basename(), | ||
| + | print(os.path.splitext(os.path.basename(filepath))[0]) | ||
| + | |||
| + | # ルートディレクトリ os.path.splitdrive() | ||
| + | print(os.path.splitdrive(filepath)[0]) | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ==== ファイル読み書き ==== | ||
| + | [[https:// | ||
| + | |||
| + | ==== フォルダ未存在時作成 ==== | ||
| + | フォルダ名は、フルパスで指定すればいい。 | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | |||
| + | new_path = " | ||
| + | if not os.path.exists(new_path): | ||
| + | os.mkdir(new_path) | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ===== HTTP クライアント ===== | ||
| + | urllibライブラリーを使用する。 | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
it技術/python.1649208242.txt.gz · 最終更新: 2022/04/06 10:24 by yajuadmin
