简体   繁体   English

IndexError:使用 for 循环时列表索引超出范围

[英]IndexError: list index out of range while using for-loop

While running for loop in below code in python getting list index out of range error.在 python 中的以下代码中运行for循环时,出现列表索引超出范围错误。 Also, please let me know if the for loop can be written in a better way.另外,如果可以用更好的方式编写 for 循环,请告诉我。

Error:- IndexError-Traceback (most recent call last)
<ipython-input-296-32d392aacb61> in <module>
---> 22         a = workbook.connections[0]

IndexError: list index out of range

Code:代码:

import tableauserverclient as TSC
import pandas as pd

tableau_auth = TSC.PersonalAccessTokenAuth('{Token_Name}',
'{Token_Secret}' , site_id="{Site}")

server = TSC.Server('Site_url',use_server_version=True)
request_options = TSC.RequestOptions(pagesize=1000)


with server.auth.sign_in(tableau_auth):

    all_workbooks_items = list(TSC.Pager(server.workbooks, request_options))
    workbook_id=[workbook.id for workbook in all_workbooks_items]

    connection2=pd.DataFrame({"Datasource_id":connection.datasource_id,
                          "Datasource_name":a.datasource_name,
                         "Workbook Name":workbook.name} , index=[0])

    for wbk_ids in workbook_id:
        workbook = server.workbooks.get_by_id(wbk_ids)
        server.workbooks.populate_connections(workbook)
        a = workbook.connections[0]
        print({a.datasource_id,a.datasource_name,workbook.name})
        connection2=connection2.append({"Datasource_id":a.datasource_id,
                                    "Datasource_name":a.datasource_name,
                                    "Workbook Name":workbook.name} ,ignore_index=True)

    server.auth.sign_out()

Instead of代替

        a = workbook.connections[0]
        print({a.datasource_id,a.datasource_name,workbook.name})
        connection2=connection2.append({"Datasource_id":a.datasource_id,
                                    "Datasource_name":a.datasource_name,
                                    "Workbook Name":workbook.name} ,ignore_index=True)

use使用

        if workbook.connections:
            a = workbook.connections[0]
            print({a.datasource_id,a.datasource_name,workbook.name})
            connection2=connection2.append({"Datasource_id":a.datasource_id,
                                    "Datasource_name":a.datasource_name,
                                    "Workbook Name":workbook.name} ,ignore_index=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 while循环-IndexError:列表索引超出范围 - while loop - IndexError: list index out of range IndexError:使用while循环列出索引超出范围 - IndexError: list index out of range with a while loop Python-IndexError:列表索引超出范围-For循环/ While循环 - Python - IndexError: list index out of range - For loop / While loop Python-IndexError:列表索引超出范围,而在for循环中 - Python- IndexError: list index out of range, while in for loop While循环-IndexError:列表分配索引超出范围 - While-loop - IndexError: list assignment index out of range Python Indexerror:使用 index() 时列表索引超出范围 - Python Indexerror: list index out of range while using index() 当使用while循环创建“list”时,我得到此错误:IndexError:列表赋值索引超出范围 - I get this error when using a while loop to make a “list” IndexError: list assignment index out of range IndexError:列表索引超出范围,在 for 循环中使用索引 - IndexError: list index out of range, using indexing in for loop 使用Tweepy获取推文时出现“ IndexError:列表索引超出范围” - “IndexError: list index out of range” while fetching tweets using Tweepy IndexError:使用多语言时列表索引超出范围 - IndexError: list index out of range in while using polyglot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM