Help us understand the problem. What is going on with this article?

TypeError: list indices must be integers, not str

More than 1 year has passed since last update.

背景

論文公開サイトarxivのスクレイピングを行い、リストの中身を操作しようとしたら
TypeError: list indices must be integers, not str
上記のようなエラーが出てきました。。
ちなみにarxiv.pyをライブラリとして使用しました。

TypeError: list indices must be integers, not str

エラーが出ているのは以下の関数ということがわかりました。(resultにはキーワードで検索した論文10個がそれぞれのタイトルごとに文字列としてリストに入っています。)

sample.py
def prune_query_result(result):
    prune_keys = ['updated_parsed',
                  'published_parsed',
                  'arxiv_primary_category',
                  'summary_detail',
                  'author',
                  'author_detail',
                  'links',
                  'guidislink',
                  'title_detail',
                  'tags',
                  'id']


    for key in prune_keys:
        try:
            del result[key]
        except KeyError:
            pass

google翻訳にかけて見たところ、
TypeError:リストインデックスは、strではなく整数でなければなりません
あ、インデックスに文字列が指定されてしまっている。。

fixed.py
def prune_query_result(result):
    prune_keys = ['updated_parsed',
                  'published_parsed',
                  'arxiv_primary_category',
                  'summary_detail',
                  'author',
                  'author_detail',
                  'links',
                  'guidislink',
                  'title_detail',
                  'tags',
                  'id']

    for i in range(len(result)):
        for key in prune_keys:
            try:
                del result[i][key]
            except KeyError:
                pass

インデックスに整数を入れてあげるとうまくいきました!!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away