mori– Author –
-
Python樹林
【Python】ファイルの文字コードを自動判定するchardetの使い方
外部システムから連携されたCSVファイルや、レガシーな環境で作成されたテキストファイルをプログラムで読み込む際、文字コード(エンコーディング)が不明であるために「文字化け」やエラーが発生することがある。 Pythonでファイルの文字コードを推定す... -
Python樹林
[Python] Converting Between Strings (str) and Bytes (bytes) and Specifying Encodings
In network communication, reading and writing binary files, or integrating with legacy systems, converting between human-readable strings (str) and computer-readable bytes (bytes) is an unavoidable process. Python uses the encode() and d... -
Python樹林
【Python】文字列(str)とバイト列(bytes)の相互変換とエンコーディング指定
ネットワーク通信やバイナリファイルの読み書き、あるいはレガシーシステムとの連携において、人間が読める「文字列(str)」と、コンピュータが扱う「バイト列(bytes)」の相互変換は避けて通れない処理です。 Pythonでは、encode() と decode() メソッ... -
Python樹林
[Python] High-Speed Full-width/Half-width Conversion with mojimoji: The Definitive Guide to Data Normalization
When handling text data containing mixed Full-width (Zenkaku) and Half-width (Hankaku) characters—such as product data on e-commerce sites or customer lists—"normalization" is an essential process to standardize inconsistent formatting. ... -
Python樹林
【Python】mojimojiで全角・半角を高速変換!データ正規化の決定版
ECサイトの商品データや顧客リストなど、全角と半角が混在するテキストデータを扱う際、表記揺れを統一する「正規化」は必須の処理です。Python標準のライブラリでも変換は可能ですが、日本語処理においては mojimoji ライブラリが圧倒的に高速で、かつ細... -
Python樹林
[Python] Safely Removing Prefixes and Suffixes with removeprefix and removesuffix
In Python 3.9 and later, dedicated methods removeprefix() and removesuffix() were added to remove specific prefixes and suffixes from strings. Previously, lstrip(), rstrip(), or slicing functionality were used for string trimming. Howeve... -
Python樹林
【Python】文字列の先頭・末尾を安全に削除するremoveprefixとremovesuffix
Python 3.9以降、文字列操作において特定の接頭辞(Prefix)や接尾辞(Suffix)を削除するための専用メソッド removeprefix() と removesuffix() が追加されました。 従来、文字列のトリミングには lstrip() や rstrip()、あるいはスライス機能が使われて... -
Python樹林
[Python] Efficiently Removing Blank Lines from Text Data
Text data read from external files or obtained via web scraping often contains unnecessary blank lines. As a preliminary step in data processing, you may want to remove these empty lines (or lines containing only whitespace) to clean up ... -
Python樹林
【Python】テキストデータから空白行を効率的に削除する方法
外部ファイルから読み込んだテキストデータや、Webスクレイピングで取得した文字列には、しばしば不要な空白行が含まれています。データ処理の前段階として、これらの空行(または空白文字のみの行)を取り除き、データを整形したい場合があります。 Pytho... -
Python樹林
[Python] How to Extract Lines Containing Specific Strings from Multi-line Text
In text data processing, such as handling log files or CSV data, you often need to extract only the lines that contain specific keywords. In Python, you can implement this process very simply and quickly by combining string splitting met... -
Python樹林
【Python】複数行のテキストから特定の文字列を含む行だけを抽出する方法
テキストデータ処理において、ログファイルやCSVデータなどの複数行テキストから、特定のキーワードが含まれる行のみを抜き出したい場面は多々あります。 Pythonでは、文字列の分割メソッドとリスト内包表記を組み合わせることで、この処理を非常に簡潔か... -
Python樹林
Converting Strings to Numbers in Python: Using int/float and Validation Logic
Data obtained from file imports or user inputs is initially treated as "strings (str)". To use this data for calculations, it must be converted to the appropriate numeric type (int or float). In this article, I will explain the basic con... -
Python樹林
Pythonで文字列を数値に変換する:int/float関数の使い方と変換可否の判定ロジック
外部ファイルからの読み込みやユーザー入力で得られたデータは、最初はすべて「文字列(str)」として扱われます。これらを計算に使用するためには、適切な数値型(int または float)に変換する必要があります。 この記事では、基本的な変換方法と、変換... -
Python樹林
Aligning Strings in Python: How to Use rjust, ljust, and center
When outputting data in a table format to the console (terminal) or formatting logs for better readability, aligning text is an essential task. Python strings come with three built-in methods to adjust the position of a string within a s... -
Python樹林
Pythonで文字列を右寄せ・左寄せ・中央寄せする:rjust, ljust, centerの使い方
コンソール(ターミナル)に表形式でデータを出力したり、ログを見やすく整形したりする際、文字の配置を揃える処理は欠かせません。 Pythonの文字列型には、指定した幅の中で文字列の位置を調整するためのメソッド rjust(), ljust(), center() が用意され... -
Python樹林
Zero-Padding Numbers in Python: How to Use zfill and f-strings
When handling ID numbers, dates, or filenames (e.g., image_001.jpg), you often need to fill the missing parts with "0" to align the number of digits. This process is called "zero-padding." In Python, there are two common ways to achieve ... -
Python樹林
Pythonで数値をゼロ埋め(ゼロパディング)する:zfillメソッドとf文字列の使い分け
ID番号や日付、ファイル名(image_001.jpgなど)を扱う際、桁数を揃えるために足りない部分を「0」で埋めたい場合があります。これを「ゼロ埋め」や「ゼロパディング」と呼びます。 Pythonでは、文字列型のメソッド zfill() を使う方法と、f-string(フォ... -
Python樹林
Splitting Strings into Lists in Python: Usage of split() and Whitespace Handling
When parsing CSV data or breaking down sentences into words, you often need to split a single long string into multiple parts according to specific rules. Python's string type provides the standard split() method for this purpose. In thi... -
Python樹林
Pythonで文字列をリストに分割する:split()メソッドの使い方と空白処理の注意点
CSVデータの解析や、文章を単語ごとに分解する処理など、1つの長い文字列を特定のルールに従って複数のパーツに分割したい場面は頻繁にあります。 Pythonの文字列型には、このための標準メソッドとして split() が用意されています。 この記事では、split(... -
Python樹林
Validating Strings in Python: How to Use isalnum, isalpha, and isdecimal
When processing user input, it is often necessary to perform validation checks, such as asking, "Is the input value only numbers?" or "Does it consist only of alphabets?" Python's string type (str) provides a set of convenient methods th... -
Python樹林
Pythonで文字列の種類を判定する:isalnum, isalpha, isdecimalなどの活用法
ユーザーからの入力を処理する際、「入力された値は数字だけか?」「アルファベットだけで構成されているか?」といったチェック(バリデーション)が必要になることはよくあります。 Pythonの文字列型(str)には、文字列の内容を検査して True または Fa... -
Python樹林
Converting Uppercase and Lowercase Strings in Python: Using upper, lower, capitalize, and title
Converting uppercase and lowercase letters is a common task, often used to standardize inconsistent user inputs for email addresses or to format article titles. Python's string type (str) provides several useful methods for performing th... -
Python樹林
Pythonで文字列の大文字・小文字を変換する:upper, lower, capitalize, titleの使い方
ユーザーが入力したメールアドレスの表記ゆれを統一したり、記事のタイトルを見やすく整形したりするために、文字列の大文字・小文字を変換する処理は頻繁に行われます。 Pythonの文字列型(str)には、このような変換を行うための便利なメソッドがいくつ... -
Python樹林
Removing Whitespace in Python Strings: Using strip, lstrip, and rstrip
Text data from user input or files often contains unintended "whitespace" or "newline codes" at the beginning or end. If these are left remaining, they can cause errors when comparing strings or saving data to a database. Python provides... -
Python樹林
Pythonで文字列の空白削除:strip, lstrip, rstripの使い方と注意点
ユーザーからの入力データやファイルから読み込んだテキスト行には、意図しない「空白(スペース)」や「改行コード」が前後についていることがよくあります。これらが残っていると、文字列の比較やデータベースへの保存時に不具合の原因となります。 Pyth... -
C#樹林
Implementing Event Notifications with IObserver and IObservable in C#
In .NET Framework and .NET Core, System.IObservable<T> and System.IObserver<T> are the standard interfaces provided for implementing the Observer Pattern. By using these interfaces, you can build a push-based notification sys... -
C#樹林
【C#】IObserverとIObservableを用いたイベント通知の実装
.NET Frameworkおよび.NET Core以降では、オブザーバーパターン(Observer Pattern)を実装するための標準インターフェースとして、System.IObservable<T> と System.IObserver<T> が用意されています。これらを利用することで、データ発行元... -
C#樹林
[C#] Implementing the IDisposable Interface to Properly Release Resources
While the .NET Garbage Collector (GC) automates memory management, it does not manage "unmanaged resources" such as file handles, database connections, or network sockets. To properly release these resources, a class must implement the I... -
C#樹林
【C#】IDisposableインターフェイスを実装してリソースを適切に解放する
.NETのガベージコレクタ(GC)はメモリ管理を自動化しますが、ファイルハンドル、データベース接続、ネットワークソケットといった「アンマネージドリソース」までは管理しません。これらのリソースを適切に解放するためには、クラスにIDisposableインター... -
C#樹林
[C#] Sorting Collections with Custom Rules by Implementing IComparer
When using the List<T>.Sort() method, there are cases where you want to sort based on a specific property or a special calculation formula, rather than the default order (e.g., ascending order). Implementing IComparable<T> on...