-
Python樹林
[Python] Writing Data to CSV Files: Using writerow vs. writerows
To save data in list format as a CSV file in Python, you use the writer object from the csv module. There are two methods: writerow(), which writes one line at a time, and writerows(), which writes multiple lines at once. You can choose ... -
Python樹林
【Python】CSVファイルにデータを書き込む:writerowとwriterowsの使い分け
Pythonでリスト形式のデータをCSVファイルとして保存するには、csv モジュールの writer オブジェクトを使用します。 1行ずつ書き込む writerow() と、複数行をまとめて書き込む writerows() があり、状況に応じて使い分けることが可能です。 ここでは、基... -
Python樹林
[Python] Basics of Reading CSV Files: Using csv.reader and Handling Headers
You can easily import CSV formatted data as lists using Python's standard csv module. This article explains the basic method of reading all lines and how to skip the first line (header) to process only the data. 1. Reading the CSV File A... -
Python樹林
【Python】CSVファイルを読み込む基本:csv.readerの使い方とヘッダー処理
Python標準ライブラリの csv モジュールを使用すると、CSV形式のデータを簡単にリストとして取り込むことができます。 ここでは、基本的な全行読み込みの方法と、1行目のタイトル(ヘッダー)を読み飛ばしてデータ部分だけを処理する方法を解説します。 1.... -
Python樹林
[Python] Comparing Dates and Times: Determining Order with Operators
In Python, datetime and date objects can be compared just like numbers using standard comparison operators (<, >, ==, !=). This allows you to determine which date is newer (future) or if they are the same. The basic rule is: "Futur... -
Python樹林
【Python】日付や時刻を比較する(大小・前後関係の判定)
Pythonの datetime や date オブジェクトは、数値と同じように 比較演算子(<, >, ==, !=) を使って、どちらが新しいか(未来か)、あるいは同じ日時かを判定できます。 基本ルールとして、「未来の日時」ほど大きく、「過去の日時」ほど小さい と... -
Python樹林
[Python] How to Check for Leap Years: Using the calendar.isleap Function
To determine if a specific year is a leap year (a year with February 29th) in Python, use the isleap() function from the calendar module. Using this function eliminates the need to write complex logic yourself (such as "divisible by 4, b... -
Python樹林
【Python】うるう年(閏年)を判定する方法:calendar.isleap関数の活用
Pythonで「ある年がうるう年(2月29日がある年)かどうか」を判定するには、calendar モジュールの isleap() 関数を使用します。 この関数を使えば、「4で割り切れるが、100で割り切れる場合は除き、ただし400で割り切れる場合はうるう年とする」といった... -
Python樹林
[Python] How to Get the Last Day of a Specific Month
To determine how many days are in a specific month (28, 30, or 31) or to check for leap years in Python, the most reliable method is to use the monthrange() function from the standard calendar library. This eliminates the need to impleme... -
Python樹林
【Python】特定年月の「月末日(月の日数)」を取得する方法
Pythonで「ある月が何日まであるか(28日, 30日, 31日)」や「うるう年かどうか」を判定するには、標準ライブラリ calendar モジュールの monthrange() 関数を使用するのが最も確実です。 これを使えば、自分で「西向く士(にしむくさむらい)」やうるう年... -
Python樹林
[Python] Converting Strings to Time Objects and Formatting
This article explains how to read "time information" from CSV data or log files, convert it into a time object that can be handled programmatically, and conversely, format it for display. We will implement code that simulates a system ma... -
Python樹林
【Python】文字列を時刻(time)オブジェクトに変換・整形する方法
PythonでCSVデータやログファイルから「時刻情報」のみを読み込み、それをプログラムで扱える time オブジェクトに変換したり、逆に表示用に任意のフォーマットへ整形したりする方法を解説します。 ここでは、工場の始業チャイム時間を管理するシステムを... -
Python樹林
[Python] Basics of Time Data: Creating time Objects and Accessing Attributes
When you want to handle "time only, without date" in Python (e.g., store opening hours, alarm settings), use the time class from the datetime module. Unlike the datetime type, the time object does not hold information about the year, mon... -
Python樹林
【Python】時刻(time)データの基本:timeオブジェクトの生成と属性取得
Pythonで「日付を含まない時刻のみ」を扱いたい場合(例:開店時間、アラーム設定時刻など)、datetime モジュールの time クラスを使用します。 datetime 型と異なり、年月日やタイムゾーン(デフォルトでは)の情報を持たず、純粋に「時・分・秒・マイク... -
Python樹林
[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 オブジェクトからは、年、月、日、時、分、秒といった個別の情報を属性(アトリビュート)として簡単に取り出すことができます...