AttributeError: ‘int’ object has no attribute ‘find’

【不具合内容】

Pythonでfind()メソッドを使用した際に発生

data = 100001111100
data.find("1111")

AttributeError                            Traceback (most recent call last)
<ipython-input-52-77008546d98e> in <cell line: 2>()
      1 data = 100001111100
----> 2 data.find("1111")

AttributeError: 'int' object has no attribute 'find'

【原因】

find()メソッドは文字列専用のメソッドで数値に対しては使えない。

【解決方法】

・文字列専用のメソッドは文字列に対して実行する

・数値から文字列に変換したあとにメソッドを実行する

data = 100001111100
str(data).find("1111")

5

コメントを残す

* 印は必須項目です。メールアドレスは公開されません。

次のHTMLタグと属性を使用することができます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください