Python AttributeError: '_io.TextIOWrapper' オブジェクトに属性 'Split' がありません
Rohan Timalsina
2023年6月21日
Python
Python AttributeError
Python Error
属性は、オブジェクトまたはクラスに関連する値です。 メソッドでサポートされていないタイプのオブジェクトの属性を呼び出すと、Python AttributeError が発生します。
たとえば、_io.TextIOWrapper オブジェクトで split() メソッドを使用すると、AttributeError が返されます。これは、_io.TextIOWrapper オブジェクトが split() メソッドをサポートしていないためです。
このチュートリアルでは、Python で AttributeError: '_io.TextIOWrapper' object has no attribute 'split' を修正する方法を説明します。
AttributeError: '_io.TextIOWrapper' object has no attribute 'split' エラーを Python で修正する
次のコマンドは、開いているファイル オブジェクトに対して split() メソッドを使用します。
f = open("test.txt")
f.split()
出力:

split() メソッドはクラス _io.TextIOWrapper の属性ではないため、AttributeError を返します。 String クラスは、split() メソッドを提供して、文字列をリストに分割します。
このエラーは、for ループを使用して修正できます。
f = open("test.txt")
for line in f:
line.split()
ファイル オブジェクトの各行は文字列であるため、エラーは返されません。
クラス _io.TextIOWrapper で利用可能なメソッドを使用して、ファイル オブジェクトを文字列に変換することもできます。
read()- このメソッドは、ファイルの内容を読み取り、文字列として返します。readline()- ファイル内の 1 行を読み取り、文字列として返します。readlines()- このメソッドは、ファイルの内容を 1 行ずつ読み取り、文字列のリストとして返すのに役立ちます。
次に、AttributeError を取得せずに split() メソッドを呼び出すことができます。
f = open("test.txt")
str = f.read()
str.split()
これで、Python で AttributeError を解決する方法がわかりました。 この記事がお役に立てば幸いです。
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: Rohan Timalsina
関連記事 - Python AttributeError
- AttributeError: Int オブジェクトに属性がありません
- AttributeError: Python の __Exit__
- Python AttributeError: _csv.reader オブジェクトに次の属性がありません
関連記事 - Python Error
- AttributeError の解決: 'list' オブジェクト属性 'append' は読み取り専用です
- AttributeError の解決: Python で 'Nonetype' オブジェクトに属性 'Group' がありません
- AttributeError: 'generator' オブジェクトに Python の 'next' 属性がありません
- AttributeError: 'numpy.ndarray' オブジェクトに Python の 'Append' 属性がありません
- AttributeError: Int オブジェクトに属性がありません
- AttributeError: Python で 'Dict' オブジェクトに属性 'Append' がありません
