Python 3.6 - TypeError:'zip'对象不可订阅

时间:2018-02-22 19:28:35

标签: python-3.x typeerror python-3.6

我的问题与this question非常相似。但是,将zip从可迭代对象转换为列表或元组的解决方案对我来说不起作用,我仍然得到同样的错误。

我的目标是在我的outFeatureClass功能使用的monthyear列表之后命名我zip的一部分。
有谁知道为什么在Python 3.6中这对我不起作用?

这是我的代码,我的问题发生在最后一行:

    import arcpy
    import datetime 
    import time
    from datetime import timedelta

    # Set environment settings
    arcpy.env.workspace = r"C:\arcGIS_Shared\Python\CenterHeatMaps6.gdb"

    #Declare variables
    fc = 'Open_GoHealth_Centers'
    fields = ['USER_market_id','USER_GoHealth_ID','USER_GoHealth_Center_Name','USER_Opening_Date', 'USER_MonthsSinceFHOpening']
    fieldname = 'USER_market_id'

    # Set Markets to loop through
    markets = [1000]

    myranges = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]#[(1,3)]#,(1,5)]
    #print (myranges)
    monthyear = ['Nov2014','Dec2014','Jan2015','Feb2015','Mar2015','Apr2015','May2015','Jun2015','Jul2015','Aug2015','Sep2015','Oct2015','Nov2015','Dec2015','Jan2016','Feb2016','Mar2016','Apr2016','May2016','Jun2016','Jul2016','Aug2016','Sep2016','Oct2016','Nov2016','Dec2016','Jan2017','Feb2017','Mar2017','Apr2017','May2017','Jun2017','Jul2017','Aug2017','Sep2017','Oct2017','Nov2017','Dec2017','Jan2018','Feb2018','Mar2018','Apr2018','May2018','Jun2018','Jul2018','Aug2018','Sep2018','Oct2018','Nov2018','Dec2018','Jan2019','Feb2019','Mar2019','Apr2019','May2019','Jun2019','Jul2019','Aug2019','Sep2019','Oct2019','Nov2019','Dec2019','Jan2020','Feb2020','Mar2020','Apr2020','May2020','Jun2020','Jul2020','Aug2020','Sep2020','Oct2020','Nov2020','Dec2020','Jan2021','Feb2021','Mar2021','Apr2021','May2021','Jun2021','Jul2021','Aug2021','Sep2021','Oct2021','Nov2021','Dec2021','Jan2022','Feb2022','Mar2022','Apr2022','May2022','Jun2022','Jul2022','Aug2022','Sep2022','Oct2022','Nov2022','Dec2022','Jan2023','Feb2023','Mar2023','Apr2023','May2023','Jun2023','Jul2023','Aug2023','Sep2023','Oct2023','Nov2023','Dec2023','Jan2024','Feb2024','Mar2024','Apr2024','May2024','Jun2024','Jul2024','Aug2024','Sep2024','Oct2024','Nov2024','Dec2024','Jan2025','Feb2025','Mar2025','Apr2025','May2025','Jun2025','Jul2025','Aug2025','Sep2025','Oct2025','Nov2025','Dec2025']
    monthname = ['November', 'December', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',]
    monthabbr = ['Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    year = [2014,2014,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2019,2019,   2019,   2019,   2019,   2019,   2019,2020,2020,2020,2020,2020,2020,2020,2020,   2020,   2020,   2020,2020,2021,2021,2021,2021,2021,2021,2021,   2021,   2021,   2021,   2021,   2021,2022,2022,2022,2022,2022,2022,2022,2022,   2022,   2022,   2022,   2022,2023,2023,2023,2023,2023,2023,2023,2023,   2023,   2023,   2023,   2023,2024,2024,2024,2024,2024,2024,2024,2024,   2024,   2024,   2024,   2024,2025,2025,2025,2025,2025,2025,2025,2025,   2025,   2025,   2025,   2025]

    month_description = zip(myranges,monthyear,monthname,monthabbr,year)
    #print(list(month_description))
    month_descr = list(month_description)

    sqlclause = (None, 'Order By USER_market_id, USER_GoHealth_ID')

    for market in markets:
        print (market)

        #Define WHERE clause statement
        whereclause = """{} = """.format(arcpy.AddFieldDelimiters(fc, fieldname)) + str(market)
    #    print (whereclause)
        for myrange in myranges:
            print (myrange)

            with arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=(None, 'ORDER BY USER_market_id, USER_GoHealth_ID')) as cursor:
                            #Loop through each row established in cursor
                            for row in (cursor):
                                # Set local variables for the FeatureClasstoFeatureClass tool
                                inFeatures = "PatientVisitsGeocoded"
                                outLocation = r"C:\arcGIS_Shared\Python\CenterHeatMaps6.gdb"
                                outFeatureClass = "PatientVisits{0}_{1}_{2}".format(row[0], row[2], month_descr[1])

1 个答案:

答案 0 :(得分:0)

在此行中,您可以创建一个zip并将其分配给month_description

month_description = zip(myranges,monthyear,monthname,monthabbr,year)

然后稍后再次对可更新内的对象应用zip,而不是列表(理论上应用的list()将立即zip对象转换为列表但不是您之前分配给month_description的那个。异常的实际原因是您尝试使用[0]迭代到zip对象:

outFeatureClass = "PatientVisits{0}_{1}_{2}".format(row[0], row[2], list(zip(month_description[1])))