mori– Author –
-
未分類
[Python] Date and Time Calculations: How to Use timedelta
To calculate dates such as "100 days later" or "3 hours ago" in Python, use the timedelta class from the standard datetime module. By adding or subtracting a timedelta object to a date or datetime object, you can perform accurate calcula... -
Python樹林
【Python】日時の計算(加算・減算):timedeltaの使い方
Pythonで「100日後の日付」や「3時間前の時刻」などを計算する場合、標準ライブラリ datetime モジュールの timedelta クラスを使用します。 date 型や datetime 型のオブジェクトに対して timedelta を足し引きすることで、カレンダー(うるう年や月末の... -
Python樹林
[Python] Getting the Current Date: Usage of date.today() and String Conversion
If you want to get only "today's date" without time information (hours, minutes, seconds) in Python, use the today() method of the date class in the datetime module. This article explains how to retrieve the current date and convert it i... -
Python樹林
【Python】現在の日付を取得する:date.today()の使い方と文字列変換
Pythonで時刻情報(時・分・秒)を含まない「今日の日付」だけを取得したい場合、datetime モジュールの date クラスにある today() メソッドを使用します。 ここでは、現在の日付を取得し、それをファイル名やログなどで使いやすい文字列フォーマットに変... -
Python樹林
[Python] Converting Between Date and String: strptime and strftime
In Python, it is very common to convert "strings to dates" (for reading from CSVs or logs) and "dates to strings" (for screen display). While you use the strptime and strftime methods just like with the datetime type, there are specific ... -
Python樹林
【Python】日付(date)と文字列を相互に変換する方法:strptimeとstrftime
Pythonで日付データ(date 型)をCSVやログから読み込むために「文字列から変換」したり、逆に画面表示のために「文字列へ変換」したりする処理は非常に頻繁に行われます。 datetime 型と同様に strptime と strftime メソッドを使用しますが、date 型特有... -
Python樹林
[Python] Basics of the date Type: Handling Year, Month, and Day
When you want to handle "only dates without time" in Python, use the date class from the datetime module. This class is suitable for scenarios where time information (hours, minutes, seconds) is unnecessary, such as birthdays, anniversar... -
Python樹林
【Python】日付データを扱うdate型の基本:年・月・日の取得と活用
Pythonで「時刻を含まない日付のみ」を扱いたい場合、datetime モジュールの date クラスを使用します。 生年月日や記念日、スケジュールの締め切り日など、時間(時・分・秒)の情報が不要なシーンで適しています。 ここでは、date オブジェクトの生成方... -
Python樹林
[Python] Handling Timezones with zoneinfo: Converting JST and UTC
Since Python 3.9, the zoneinfo module in the standard library is recommended for timezone processing. This allows accurate handling of regional timezones like Japan Standard Time (Asia/Tokyo) and Coordinated Universal Time (UTC). This ar... -
Python樹林
【Python】タイムゾーンを扱うzoneinfoの使い方:JSTとUTCの変換
Python 3.9以降、日付と時刻のタイムゾーン処理には標準ライブラリの zoneinfo モジュールを使用するのが推奨されています。 これにより、日本時間(Asia/Tokyo)や協定世界時(UTC)など、地域ごとのタイムゾーン情報を正確に扱えるようになります。 この... -
Python樹林
[Python] Converting Datetime to ISO 8601 Strings: Handling Timezones
When exchanging date and time data as strings for Web APIs or databases, the international standard ISO 8601 format (e.g., 2025-12-15T10:00:00+09:00) is widely used. To convert a Python datetime object into this format, you use the isofo... -
Python樹林
【Python】日時をISO 8601形式の文字列に変換する方法:タイムゾーン対応
Web APIとの連携やデータベースへの保存において、日時データを文字列としてやり取りする際は、国際標準規格である「ISO 8601形式」(例: 2025-12-15T10:00:00+09:00)が広く利用されます。 Pythonの datetime オブジェクトをこの形式に変換するには、isof... -
Python樹林
[Python] Getting Current Date/Time and Formatting as String
When recording log timestamps or including dates in filenames in Python, the standard pattern is to combine the now() and strftime() methods of the datetime class. This article explains how to get the current date and time (Year, Month, ... -
Python樹林
【Python】現在日時を取得して任意の形式の文字列に変換する方法
Pythonでログの出力時刻を記録したり、ファイル名に日時を含めたりする場合、datetime クラスの now() メソッドと strftime() メソッドを組み合わせるのが基本パターンです。 ここでは、現在の日時(年月日時分秒)を取得し、それを「2025-12-15 10:30:00... -
Python樹林
[Python] Creating datetime Objects and Accessing Attributes (Year, Month, Day)
To define specific date and time data within a Python program, you use the constructor of the datetime class. From the created datetime object, you can easily extract individual information such as year, month, day, hour, minute, and sec... -
Python樹林
【Python】datetimeオブジェクトの生成と年・月・日などの属性取得方法
Pythonで特定の日時データをプログラム内で定義する場合、datetime クラスのコンストラクタを使用します。 作成した datetime オブジェクトからは、年、月、日、時、分、秒といった個別の情報を属性(アトリビュート)として簡単に取り出すことができます... -
Python樹林
[Python] 4 Key Types of the datetime Module and How to Use Them
Python's standard datetime module allows you to handle dates and times. It consists of four main data types (classes) that should be used according to your purpose. Understanding the role and differences of each is essential for accurate... -
Python樹林
【Python】日付・時間を扱うdatetimeモジュールの主要4型と活用法
Pythonで日付や時間を扱う標準ライブラリ datetime モジュールには、目的に応じて使い分けるべき4つの主要なデータ型(クラス)が存在します。 それぞれの役割と違いを理解することで、スケジュールの計算やログの記録などを正確に実装できます。 主要なデ... -
Python樹林
[Python] How to Merge Two Dictionaries: Using the update Method and dict Function
When you want to combine two dictionaries into one in Python, there are two approaches: a "destructive" method that updates the original dictionary, and a "non-destructive" method that creates a new dictionary. This article explains how ... -
Python樹林
【Python】2つの辞書をマージ(結合)する方法:updateメソッドとdict関数の活用
Pythonで2つの辞書(dictionary)を結合して1つにまとめたい場合、元の辞書を更新する「破壊的」な方法と、新しい辞書を作成する「非破壊的」な方法があります。 それぞれの実装方法と、キーが重複した場合の挙動について解説します。 1. update() メソッ... -
Python樹林
[Python] How to Swap Dictionary Keys and Values: Using Dictionary Comprehension
In a Python dictionary, you may sometimes want to reverse the relationship between Keys and Values. This is useful when you want to identify a Key based on its Value, known as a "reverse lookup," rather than the standard search (Key to V... -
Python樹林
【Python】辞書のキーと値を入れ替える(逆引き辞書を作る)方法:内包表記の活用
Pythonの辞書(dictionary)において、キー(Key)と値(Value)の関係を逆転させたい場合があります。これは、通常の検索(キーから値を引く)ではなく、値からキーを特定したい場合(逆引き)に有用です。 辞書内包表記(Dictionary Comprehension)を使... -
Python樹林
[Python] Efficiently Creating a Dictionary from Two Lists (Keys and Values)
In Python, when you have separate lists for keys and values, the most standard and elegant way to combine them into a single dictionary is to use the zip() function. There is no need to write a for loop to add items one by one. You can i... -
Python樹林
【Python】キーと値の2つのリストから辞書を効率的に作成する方法
Pythonにおいて、キー(Key)となるリストと、それに対応する値(Value)となるリストが別々に存在する場合、これらを結合して一つの辞書(ディクショナリ)を作成するには zip() 関数を使用するのが最も一般的でスマートな方法です。 for文でループを回し... -
Python樹林
[Python] Removing Duplicates from a List While Preserving Order
When you want to remove duplicate data from a list, using the set type set() is common. However, set() does not maintain the order of elements. If you want to remove duplicates while keeping the original order of data appearance, using t... -
Python樹林
【Python】リストから重複要素を削除しつつ順序を保持する方法
リスト内の重複データを取り除きたい場合、集合型 set() を使うのが一般的ですが、set() は要素の並び順を保持しません。 データの出現順序を維持したまま重複だけを取り除きたい場合は、辞書型(dict)の特性を利用する dict.fromkeys() メソッドが最適で... -
Python樹林
[Python] Randomly Sorting Lists: Using shuffle vs. sample
To randomly shuffle list elements in Python, there are two methods in the random module: the sample() function and the shuffle() function. While both achieve the same result of "reordering," they differ in behavior regarding whether they... -
Python樹林
【Python】リストをランダムに並び替える:shuffleとsampleの使い分け
Pythonでリストの要素をランダムにシャッフルする(順序をごちゃ混ぜにする)には、random モジュールの sample() 関数を使う方法と shuffle() 関数を使う方法の2通りがあります。 これらは「並び替える」という結果は同じですが、元のリストを変更するか... -
Python樹林
[Python] 3 Ways to Reverse a List
In Python, there are three main approaches to reversing the order of a list. These methods differ in behavior: some create a new list, others modify the original list, and one handles data as an iterator. It is important to choose the ri... -
Python樹林
【Python】リストを逆順(リバース)に並び替える3つの方法
Pythonでリストの並び順を反対(逆順)にしたい場合、主に3つのアプローチがあります。 それぞれ「新しいリストを作成するか」「元のリストを書き換えるか」「イテレータとして扱うか」という点で挙動が異なるため、用途に合わせて使い分けることが重要で...