TypeError: 'dict_keys' object does not support indexing

エラーTypeError

エラーメッセージ

 状態:-  閲覧数:1,353  投稿日:2017-11-05  更新日:2017-11-11  
「dict_keysオブジェクト」はインデックスをサポートしていない
[error] TypeError: 'dict_keys' object does not support indexing, referer

エラー原因


「dict_keysオブジェクト」へ「インデックスアクセス」しようとしたことによるエラー
・リストへなら「インデックスアクセス」可

背景


辞書のkeys()メソッドは、バージョンにより動作が異なる

Python2系
・リストを返す

Python3系
・dict_keysを返す

エラー対応


「インデックスアクセス」するためには、「dict_keysオブジェクト」をリストへ変換する必要がある

実際に遭遇した例

 閲覧数:375 投稿日:2017-11-05 更新日:2017-11-05 

エラーメッセージ


Apacheログ
[error] [client 123.456.78.901] TypeError: 'dict_keys' object does not support indexing, referer

エラー発生箇所


▼index.py
lookup_key = item.attrib.keys()[0]


エラー修正内容


▼index.py
lookup_key = list(item.attrib.keys())[0]



TypeError: must be str, not set

コメント投稿(ログインが必要)



週間人気ページランキング / 6-21 → 6-27
順位 ページタイトル抜粋 アクセス数
1 Python用語 16
2 インポート | インポートシステム 1
2 AttributeError: type object 'datetime.datetime' has no attribute 'datetime' | AttributeError(エラー) 1
2026/6/28 5:05 更新