#author("2023-01-20T11:18:47+09:00","default:irrp","irrp")
#author("2023-01-23T21:45:48+09:00","default:irrp","irrp")
→Python関連

→Pandas関連

#contents


*サブトピック [#g4de9318]
-Python文法系Tips
-PythonでExcel制御
-Pythonのロギング


*一般 [#j430a113]
-[[[小ネタ] Pythonでライブラリを使わずクリップボードへテキストを張り付ける | DevelopersIO>https://dev.classmethod.jp/articles/python-copy-to-clipboard-without-library/]] 2023.1

-Pythonで超簡単にWebサーバ立てる
--任意のフォルダで下記のコマンドを実行すると、自分のマシンのポート8888で簡易的にWebサーバを動かせる。
 python -m http.server 8888

-[[AIエンジニアが気をつけたいPython実装のノウハウ・コツまとめ - Qiita>https://qiita.com/sugulu_Ogawa_ISID/items/c0e8a5e6b177bfe05e99]] 2021

-[[VSCodeでPython書いてる人はとりあえずこれやっとけ〜 - Qiita>https://qiita.com/nanato12/items/ddf26487eb30714251c3]] 2022.8

-[[Python: ファイルの更新日時(タイムスタンプ)を取得する>https://step-learn.com/article/python/119-file-timestamp.html]] 2022.7
 ts = os.path.getmtime(file_path)
 print(ts)       # 1657734360.18843
 print(type(ts)) # <class 'float'>
 # datetime型に変換
 d = datetime.datetime.fromtimestamp(ts)

-[[Argparse チュートリアル &#8212; Python 3.11.0b5 ドキュメント>https://docs.python.org/ja/3/howto/argparse.html]] 2022.11
--引数のパースは自前で作る必要はない

-[[sys.path.append() を使わないでください - Qiita>https://qiita.com/siida36/items/b171922546e65b868679]] 2020

-[[[Python] inspectを使ってスタックトレースを表示する - Qiita>https://qiita.com/Test_test/items/b097ac73706be026943f]] 2017

-[[実行中の関数・メソッド名を取得したい - Qiita>https://qiita.com/megmogmog1965/items/0b4ea3d58e34f1854158]] 2015

-[[__pycache__とはいったいなんなのか? - Qiita>https://qiita.com/Kobayashi2019/items/03e31ee50b924f428e71]] 2019

-[[Python3で数値文字参照をUNICODE文字に変換する方法>https://weblabo.oscasierra.net/python/python3-unescape-numeric-character-reference.html]] 2022.9
 print(html.unescape("&#21556; ほげほげ"))

-[[文字列の中から、\uではじまる記号が現れたり消えたり…>https://teratail.com/questions/83502]] 2017
--ごくごく単純な話でprintにリストを渡しているので、リストの__repr__が表示されているだけです。

-[[\ufeffって??? - Qiita>https://qiita.com/msk02/items/c3a1c4a1e1ef94c37228]] 2018
--テキストを読み込むときにBOMありutf8だとファイルの先頭にBOMがある。BOMをデータと認識しないようにencoding を utf-8-sig とする必要がある。

-[[【Python】テストしやすいコードに近づけるための初歩 - Qiita>https://qiita.com/kjm_nuco/items/3422d589ef05d7cf61fe]] 2022.9

-[[7 Bad Habits That Every Python Programmer Should Avoid | by Pralabh Saxena | Sep, 2022 | Level Up Coding>https://levelup.gitconnected.com/7-bad-habits-that-every-python-programmer-should-avoid-d477ea91e7da]] 2022.9

-[[Python の高速化 - MicroAd Developers Blog>https://developers.microad.co.jp/entry/2022/08/08/100000]] 2022.8

-[[ゼロからはじめるPython(92) PythonとGraphvizで簡単 - 手順書グラフを作ろう | TECH+>https://news.mynavi.jp/techplus/article/zeropython-92/]] 2022.7

-[[【Python】良い書き方と悪い書き方を知って中級者を目指す&#128013; - Qiita>https://qiita.com/_masa_u/items/b21c493a82e8aba8993f]] 2022.7

-[[sys.path.append() を使わないでください - Qiita>https://qiita.com/siida36/items/b171922546e65b868679]] 2022.6

-[[[Tips] Python で URL で指定した JSON を読み込んで日付型で絞り込む ( Pandas 使用 ) | DevelopersIO>https://dev.classmethod.jp/articles/filter-json-elements-by-date-type-with-pandas-on-python/]] 2022.4

-[[きれいなPythonプログラミング(パイソニックなコードを書こう)を読んでみた - Qiita>https://qiita.com/inetcpl/items/af42b8221d9447637449]] 2022.3

-[[あなたが知らないかもしれないPythonのTIPS 5つ | BLOG - DeNA Engineering>https://engineering.dena.com/blog/2022/03/5-python-tips-you-might-not-know/]] 2022.3

-[[Python で並行処理を駆使して実行時間を 40% 短縮する方法 - MOTEX TECH BLOG>https://tech.motex.co.jp/entry/2022/06/30/083000]] 2022.6
-[[Pythonの並列処理・並行処理をしっかり調べてみた - Qiita>https://qiita.com/simonritchie/items/1ce3914eb5444d2157ac]] 2019

-[[Pythonのバージョンを確認、表示(sys.versionなど) | note.nkmk.me>https://note.nkmk.me/python-sys-platform-version-info/]] 2017

-[[Python clickの案外知られていない機能 - Qiita>https://qiita.com/cvusk/items/fcda5e77350f248d39e5]] 2022.1

-[[Pythonならわかるシステムプログラミング - Qiita>https://qiita.com/shibukawa/items/6856b932d87f42a678c7]] 2021.12

-[[[Python] 特殊メソッドまとめ - Qiita>https://qiita.com/yoshi-kin/items/26fa4194ee7a726d685c]] 2021.11

-[[Pythonで為替レートを取得しチャートとして可視化するまでの手順>https://qiita.com/kazama1209/items/c5d7fc2e783050bbe211]] 2021.3

-[[ast --- 抽象構文木 &#8212; Python 3.10.6 ドキュメント>https://docs.python.org/ja/3/library/ast.html]] 2022.9
-[[dis --- Python バイトコードの逆アセンブラ &#8212; Python 3.10.6 ドキュメント>https://docs.python.org/ja/3/library/dis.html]] 2022.9
-[[たった1行から始めるPythonのAST(抽象構文木)入門>https://qiita.com/rhoboro/items/5bbaa3fa659ed7454d56]] 2020.12

-[[Pythonの「痒いところに手が届くなぁ・・・」と思う機能>https://qiita.com/_akiyama_/items/b24acf53470a885c70db]] 2020.3

-[[知っているようで意外と知らなかったPython小ネタ集>https://dev.classmethod.jp/server-side/python/python-tips-best5/]] 2018.9

-[[機械学習の Python との出会い>http://www.kamishima.net/mlmpyja/]] 2012.5.10
--このチュートリアルでは,いろいろな機械学習の手法を Python で実装してゆくことで, NumPy や SciPy など科学技術計算に関連したモジュールの具体的な使い方を説明します




*設定ファイル [#p3f7a182]
-[[Pythonで簡単に設定ファイルを使う。書き換え可能で、配列も辞書型も使う方法 - When it’s ready.>https://a2c.hatenablog.com/entry/20100816/1281891273]] 2010
-[[【Python】configファイルの読み込み・書き込みはconfigparserを使おう - 猫が蛇と戯れる>https://neko-py.com/python-configparser]] 2019
-[[Pythonの設定ファイル管理まとめ(settings / ini / json 形式別) [コピペサンプルコード付き] | KodoCode>https://kodocode.net/python-begin-settings/]] 2021


*ディレクトリ/ファイル操作 [#y0918c0d]
-[[Python最新情報と勉強法(24) Pythonをローカル環境で開発するなら気を付けたいこととpathlibのすすめ | TECH+(テックプラス)>https://news.mynavi.jp/techplus/kikaku/python_learn-24/]] 2023.1

-[[Pythonで条件を満たすパスの一覧を再帰的に取得するglobの使い方 | note.nkmk.me>https://note.nkmk.me/python-glob-usage/]] 2018
 glob(r"order_pdf\**\*.*", recursive=True)

-[[Pythonでフォルダ内のファイルリストを取得する - Qiita>https://qiita.com/amowwee/items/e63b3610ea750f7dba1b]] 2017

-[[Python | ファイル名またはディレクトリ名を変更する>https://www.javadrive.jp/python/file/index16.html]] 2022
-[[【python】ファイルの条件を指定して一括移動する方法【コード解説】 | のんびり自習部屋>https://yuruikurashi.com/%e3%80%90python%e3%80%91%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e6%9d%a1%e4%bb%b6%e3%82%92%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%a6%e4%b8%80%e6%8b%ac%e7%a7%bb%e5%8b%95%e3%81%99%e3%82%8b%e6%96%b9/]] 2021
-[[【Python入門】ディレクトリを簡単に作成する|os.mkdir・os.makedirs | 侍エンジニアブログ>https://www.sejuku.net/blog/63634]] 2022.9
--os.makedirs で深いところまで一回で作れる。すでにあってもエラーにならない



*日付処理 [#ydfe775d]
-[[strftimeメソッドでエラー(UnicodeEncodeError)が表示される - ガンマソフト>https://gammasoft.jp/support/python-datetime-strftime-unicodeencodeerror/]] 2019
--このエラーは、一般的な日本語版のWindowsでは発生しません。日本語版以外のWindowsパソコンをお使いの場合や言語・ロケールの設定を変更している場合に発生します。
--Pythonはstrftime()メソッドを処理するのに、Windows内のライブラリを使用します。そのため、Windowsシステムが日本語以外の場合、書式("%Y年%m月%d日")の中の日本語をエンコードできないので、UnicodeEncodeErrorを投げます。
 import locale
 locale.setlocale(locale.LC_CTYPE, "Japanese_Japan.932")
 
 まだエラーが表示される場合はこちらも試してください。
 locale.setlocale(locale.LC_CTYPE, "English_United States.932")

-[[Pythonのdatetimeで日付の範囲を比較演算子で指定する - Qiita>https://qiita.com/Alice1017/items/4ce5be3f46aa34f9f900]] 2017

-[[【Python】文字列⇒日付、日付⇒文字列の変換(str, datetime)(備忘録) - Qiita>https://qiita.com/shinkai_/items/7a0e7e8c717f6001d366]] 2021.3

-[[Stop using utcnow and utcfromtimestamp>https://blog.ganssle.io/articles/2019/11/utcnow.html]] 2019
--datetime.utcnow()と datetime.utcfromtimestamp()は使うべからず。これはあなたの期待しているものではない。それよりもタイムスタンプ自身にUTCであることを認識させる datetime.now(tz=timezone.utc)と datetime.fromtimestamp(ts, tz=timezone.utc)を使うべし。

-[[Pythonにおける日本(東京)時刻の取得 ※Google colabでdatetimeを使用 - Qiita>https://qiita.com/hinata1119/items/0835dea5d16b70ea2b80]] 2022.11

-[[メモ:pythonで月末日を取得する方法 (月初日も追記) - Qiita>https://qiita.com/peketamin/items/c38de79e2a3946ec2358#datetimetimedeltadays1-%E3%82%92%E4%BD%BF%E3%81%86%E6%96%B9%E6%B3%95]] 2014
 import calendar
 print calendar.monthrange(2014,12)

-[[Pythonで月初・月末(初日・最終日)、最終X曜日の日付を取得 | note.nkmk.me>https://note.nkmk.me/python-datetime-first-last-date-last-week/]] 2019

-[[Pythonのdatetimeで日付や時間と文字列を変換(strftime, strptime) | note.nkmk.me>https://note.nkmk.me/python-datetime-usage/]] 2022.9
-[[Pythonで経過時間や日時(日付・時刻)の差分を測定・算出 | note.nkmk.me>https://note.nkmk.me/python-datetime-timedelta-measure-time/]] 2019
-[[Python 日付、時刻の処理 - Qiita>https://qiita.com/xza/items/9618e25a8cb08c44cdb0#%E6%97%A5%E4%BB%98%E3%81%AE%E5%8A%A0%E7%AE%97%E6%B8%9B%E7%AE%97]] 2018

-dateutil を使うには別途インストールが必要
--[[How to use dateutil.relativedelta in Python 3.x? - Stack Overflow>https://stackoverflow.com/questions/38792126/how-to-use-dateutil-relativedelta-in-python-3-x]] 2011

*文字列処理 [#vf28c157]
-[[Pythonの文字列の改行方法と便利な操作まとめ | HEADBOOST>https://www.headboost.jp/python-n-strings/]] 2021
 list = ["text1", "text2", "text3"]
 string = "\n".join(list)

-[[【Python】 文字列定数について(stringモジュール) | Hbk project>https://hibiki-press.tech/python/built-in-types/string-constants/4104]] 2019

-[[Pythonのf文字列(フォーマット済み文字列リテラル)の使い方 | note.nkmk.me>https://note.nkmk.me/python-f-strings/]] 2018

-[[Pythonのf文字列の使い方 - ガンマソフト株式会社>https://gammasoft.jp/blog/python-f-string/]] 2021.9

-[[【python3】ファイル名に使用出来ない文字を変換する。 | [python-auto] 自動化の備忘録ブログ>https://python-auto.com/2020/07/12/%E3%80%90python3%E3%80%91%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D%E3%81%AB%E4%BD%BF%E7%94%A8%E5%87%BA%E6%9D%A5%E3%81%AA%E3%81%84%E6%96%87%E5%AD%97%E3%82%92%E5%A4%89%E6%8F%9B%E3%81%99%E3%82%8B/]] 2020.7
 file_name = re.sub(r'[\\|/|:|?|.|"|<|>|\|]', '-', file_name)

-[[図解!Python 正規表現の徹底解説!(文字列の抽出と置換など) - AI-interのPython3入門>https://ai-inter1.com/python-regex/]] 2022.9
-[[Pythonの文字列フォーマット(formatメソッドの使い方) - ガンマソフト株式会社>https://gammasoft.jp/blog/python-string-format/]] 2022.9


*PDFファイル [#gfa7e248]
→PDF関連へ


*CSVファイル [#n4269472]
-[[pythonでのcsvファイルの読み込み - Qiita>https://qiita.com/motoki1990/items/0274d8bcf1a97fe4a869]] 2022.9

-CSVを辞書のリストに読み込んで特定の項目でソートするサンプル
 import csv
 
 rows =[];
 #BOMありUTF8 なのでエンコーディングは utf-8-sig とした
 with open('./Anken.csv', 'r', encoding='utf-8-sig', errors='strict', newline='') as csv_file:
     f = csv.DictReader(csv_file, delimiter=',', doublequote=True, lineterminator='\r\n', quotechar='"', skipinitialspace=True)
     for row in f:
         rows.append(row)
 
 #破壊型ソートです
 rows.sort(key=lambda x:x['JOBNo.'])
 for row in rows:
     print(row['JOBNo.'], row['案件名'])



*print()文 [#ib914079]
-例:print('{}の税込価格は{:,d}円です'.format(fruit, fruit_price))
-[[printf 形式の文字列書式化 | 組み込み型 &#8212; Python 3.9 ドキュメント>https://docs.python.org/ja/3.9/library/stdtypes.html#printf-style-string-formatting]] 2022.4
--format % values (format は文字列) とすると、format 中の % 変換指定は values 中のゼロ個またはそれ以上の要素で置換されます。
--format が単一の引数しか要求しない場合、 values はタプルでない単一のオブジェクトでもかまいません。 
--それ以外の場合、 values はフォーマット文字列中で指定された項目と正確に同じ数の要素からなるタプルか、単一のマップオブジェクトでなければなりません。


* exe化 [#ea031166]
-exe化することでプログラムの速度が遅くなる可能性があります。exe化されたプログラムは、Pythonインタプリタが持つ高い最適化機能を持っていないため。
-また、exe化されたプログラムは、外部ライブラリやモジュールが含まれているため、起動時間が長くなることもあります。
-スピードを上げるには以下の方法が考えられます。
--不要なモジュールやライブラリを削除する
--アプリケーションの起動時間を短縮するために、py2exeのオプションを調整する
--CythonなどでPythonコードをコンパイルし、exe化する


-[[PyInstaller より圧倒的に優れている Nuitka の使い方とハマったポイント | つくみ島だより>https://blog.tsukumijima.net/article/python-nuitka-usage/]] 2022

-[[The structure of .pyc files | Ned Batchelder>https://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html]] 2008

-[[DeprecationWarning: the `setup.py py2exe` command line interface is deprecated &#183; Issue #150 &#183; py2exe/py2exe &#183; GitHub>https://github.com/py2exe/py2exe/issues/150]] 2022
--[[py2exe/migration.md at master &#183; py2exe/py2exe &#183; GitHub>https://github.com/py2exe/py2exe/blob/master/docs/migration.md]] 2022.9
--[[py2exe/py2exe.freeze.md at master &#183; py2exe/py2exe &#183; GitHub>https://github.com/py2exe/py2exe/blob/master/docs/py2exe.freeze.md]] 

-[[py2exe公式>http://www.py2exe.org/index.cgi/FrontPage]] 
-[[python - Multiple top-level packages discovered in a flat-layout - Stack Overflow>https://stackoverflow.com/questions/72294299/multiple-top-level-packages-discovered-in-a-flat-layout]] 2022
--下記のようなエラーが出る場合、setup() の引数に py_modules=[] を追加すると回避できる場合がある
 error: Multiple top-level packages discovered in a flat-layout: ['xxx'...].
 
 To avoid accidental inclusion of unwanted files or directories,
 setuptools will not proceed with this build.
 
 If you are trying to create a single distribution with multiple packages
 on purpose, you should not rely on automatic discovery.
 Instead, consider the following options:
 
 1. set up custom discovery (`find` directive with `include` or `exclude`)
 2. use a `src-layout`
 3. explicitly set `py_modules` or `packages` with a list of names
 
 To find more information, look for "package discovery" on setuptools docs.


-[[python - py2exe - generate single executable file - Stack Overflow>https://stackoverflow.com/questions/112698/py2exe-generate-single-executable-file]] 2022.10

-[[python py2exeでexe化した後、pyside周りとかでエラーがでた場合の対処のメモ &#8211; SAKAIDEN>https://www.sakaiden.com/?p=31200]] 2022.10

-[[pyhon.py→exe化→軽量化→高速化 忘備録(pyinstaller py2exe) | by Yutaka_kun | LSC PSD | Medium>https://medium.com/lsc-psd/pyhon-py-exe%E5%8C%96-%E8%BB%BD%E9%87%8F%E5%8C%96-%E9%AB%98%E9%80%9F%E5%8C%96-%E5%BF%98%E5%82%99%E9%8C%B2-pyinstaller-py2exe-4cd7c5f70c99]] 2020

-[[エクセルをPythonで操作したい!&#12316;xlwings×Pyinstaller&#12316;|分析屋|note>https://note.com/bunsekiya_tech/n/n16a4573b9a76]] 2022.9
--Python環境が無い&#12040;にまるっとPython+ライブラリ+実&#12175;コードを渡す「Pyinstaller」。エクセルとPythonを連携させる「xlwings」。
--2つを連携させることでPython×エクセルアプリが誰にでも配布できるようになりました。

-[[Pythonファイルを超簡単にexeファイル化するGUIソフト - Qiita>https://qiita.com/osorezugoing/items/4ea5249c43c0ba8b89aa]] 2022.6
-[[Pythonスクリプトのexe化>https://qiita.com/ybsh2891/items/afd741153593d2d9e021]] 2021.7


*メール送信 [#ha54ad85]
-[[Pythonで複数の宛先にメールを送信する(Python3) - Qiita>https://qiita.com/bi-nonet/items/5104978238f1f5e72cc8]] 2014
-[[Pythonでメール送信 | Python学習講座>https://www.python.ambitious-engineer.com/archives/2034]] 2020



*pip [#z340a947]
-[[Why you should use `python -m pip`>https://snarky.ca/why-you-should-use-python-m-pip/]] 2019
-- this blog post is meant to explain what python -m pip is and why you should be using it when you run pip.

-[[Google Colabでpip installしたものを消えないようにする - Qiita>https://qiita.com/ezoalbus/items/d42d47685b592816054d]] 2021

-[[pip - How do I install Python packages in Google's Colab? - Stack Overflow>https://stackoverflow.com/questions/51342408/how-do-i-install-python-packages-in-googles-colab]] 2018
--Colab is just like a Jupyter notebook. Therefore, we can use the ! operator here to install any package in Colab. What ! actually does is, it tells the notebook cell that this line is not a Python code, its a command line script. So, to run any command line script in Colab, just add a ! preceding the line.


*デバッグ [#d4943373]
-VSCode でライブラリの中でブレークしたい場合は、launch.json で configurations[n].justMyCode をtrue に設定するとよい。
 "justMyCode": false,


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS