Ошибка IndexError, когда между индексом и (i) возникает несоответствие длины - PullRequest
4 голосов
/ 08 января 2020

я новичок в SO и python. Я сталкиваюсь с этой ошибкой. Я понимаю, что мои len (i) и len (клиенты) разные. Но я не уверен, где мне нужно изменить свой код, чтобы я мог перестать получать эту ошибку индекса списка вне диапазона:

Мой код:

import geocoder
import requests
import time
import pandas as pd
import pyodbc
from geocodio import GeocodioClient

df = pd.read_sql_query("SELECT TOP 2000 [StreetAddressLine1],[StateAbbreviation],[ZipCode],[Latitude],[Longitude],[LocationKey] FROM Dim.Location WHERE AttemptToGeocode = 1 ORDER BY CASE WHEN GeocodeSource = 'Unknown' THEN 1 WHEN GeocodeSource = 'Esri' THEN 2 WHEN GeocodeSource = 'Redpoint' THEN 2 END", conn)

# Set the input and output files
input_file_path = "df"
output_file_path = "StgDimLocationEsriThree"  # appends "####.csv" to the file name when it writes the file.

# Set the name of the column indexes here so that pandas can read the CSV file
address_column_name = "StreetAddressLine1"
state_column_name = "StateAbbreviation"
zip_column_name = "ZipCode"   # Leave blank("") if you do not have zip codes
#VetId = "VetId"
# Where the program starts processing the addresses in the input file
# This is useful in case the computer crashes so you can resume the program where it left off or so you can run multiple
# instances of the program starting at different spots in the input file
start_index = 0
# How often the program prints the status of the running program
status_rate = 1
# How often the program saves a backup file
write_data_rate = 500000
# How many times the program tries to geocode an address before it gives up
attempts_to_geocode = 10
# Time it delays each time it does not find an address
# Note that this is added to itself each time it fails so it should not be set to a large number
wait_time = 3

# ----------------------------- Processing the input file -----------------------------#

#df = pd.read_csv(input_file_path, low_memory=False,encoding="utf-8")
# df = pd.read_excel(input_file_path)

# Raise errors if the provided column names could not be found in the input file
if address_column_name not in df.columns:
    raise ValueError("Can't find the address column in the input file.")
if state_column_name not in df.columns:
    raise ValueError("Can't find the state column in the input file.")

# Zip code is not needed but helps provide more accurate locations
if (zip_column_name):
    if zip_column_name not in df.columns:
        raise ValueError("Can't find the zip code column in the input file.")
    addresses = (df[address_column_name] + ', ' + df[zip_column_name].astype(str) + ', ' + df[state_column_name]).tolist()
else:
    addresses = (df[address_column_name] + ', ' + df[state_column_name]).tolist()



# ----------------------------- Function Definitions -----------------------------#

# Creates request sessions for geocoding
class GeoSessions:
    def __init__(self):
        self.Arcgis = requests.Session()
        self.Komoot = requests.Session()


# Class that is used to return 3 new sessions for each geocoding source
def create_sessions():
    return GeoSessions()


# Main geocoding function that uses the geocoding package to covert addresses into lat, longs
def geocode_address(address, s):
    g = geocoder.arcgis(address, session=s.Arcgis)
    if (g.ok == False):
        g = geocoder.komoot(address, session=s.Komoot)

    return g


def try_address(address, s, attempts_remaining, wait_time):
    g = geocode_address(address, s)
    if (g.ok == False):
        time.sleep(wait_time)
        s = create_sessions()  # It is not very likely that we can't find an address so we create new sessions and wait
        if (attempts_remaining > 0):
            try_address(address, s, attempts_remaining-1, wait_time+wait_time)
    return g


# Function used to write data to the output file
def write_data(data, index):
    file_name = (output_file_path)
    print("Created the file: " + file_name + ".csv")
    done = pd.DataFrame(data)
    done.columns = ['Latitude', 'Longtitude','LocationKey','MatchDescription']
    done.to_csv((file_name + ".csv"), sep=',', encoding='utf8')


# Variables used in the main for loop that do not need to be modified 
s = create_sessions()
results = []
failed = 0
total_failed = 0
progress = len(addresses) - start_index



# ----------------------------- Main Loop -----------------------------#

for i, address in enumerate(addresses[start_index:]):


    # Print the status of how many addresses have be processed so far and how many of the failed.
    if ((start_index + i) % status_rate == 0):
        total_failed += failed
        print(
            "Completed {} of {}. Failed {} for this section and {} in total.".format(i + start_index, progress, failed,
                                                                                     total_failed))
        failed = 0

    # Try geocoding the addresses
    try:
        g = try_address(address, s, attempts_to_geocode, wait_time)
        if (g.ok == False):
            results.append([address, "was", "not", "geocoded"])
            print("Gave up on address: " + address)
            failed += 1
        else:
            results.append([address, g.latlng[0], g.latlng[1], df["LocationKey"].loc[i]])

    # If we failed with an error like a timeout we will try the address again after we wait 5 secs
    except Exception as e:
        print("Failed with error {} on address {}. Will try again.".format(e, address))
        try:
            time.sleep(5)
            s = create_sessions()
            g = geocode_address(address, s)
            if (g.ok == False):
                print("Did not find it.")
                results.append([address, "was", "not", "geocoded"])
                failed += 1
            else:
                print("Successfully found it.")
                results.append([address, g.latlng[0], g.latlng[1],df['LocationKey'].values[0]])
        except Exception as e:
            print("Failed with error {} on address {} again.".format(e, address))
            failed += 1
            results.append([address, e, e, "ERROR"])
#matchaccuracy app integration
API_KEY = "xyz"
client = GeocodioClient(API_KEY)
results1 = [i[0] for i in results]
customers = results1 
customers = pd.DataFrame(customers)
customers['address_string'] = customers[0].map(str)

geocoded_acuracy = []
geocoded_acuracy_type = []
geocoded_formattedaddress = []

for address in customers['address_string'].values:
    geocoded_address = client.geocode(address)
    accuracy_type = geocoded_address['results'][0]['accuracy_type']    
    geocoded_acuracy_type.append(accuracy_type)

customers['accuracy_type'] = geocoded_acuracy_type
MatchDecription = customers[['accuracy_type']]
results = pd.DataFrame(results)
results = results.drop([0],axis=1)
MtDf = pd.DataFrame(MatchDecription.values.tolist())
results = pd.concat([results, MtDf], axis=1)

    # Writing what has been processed so far to an output file

if (i%write_data_rate == 0 and i != 0):
        write_data(results, i + start_index)

    # print(i, g.latlng, g.provider)

# Finished
write_data(results, i + start_index + 1)
print("Finished! :)")

Полученная ошибка находится в строке:

accuracy_type = geocoded_address['results'][0]['accuracy_type'] 

IndexError: индекс списка выходит за пределы диапазона

, если я выполню печать (i), получу 1999, а len (клиенты) и len (результаты) равны 2000. Что мне сделать, чтобы отладить эту ошибку?

...