Stack Overflow Asked by user13669275 on January 22, 2021
My Code:
def email_address_grab(email_list):
""" This function takes in a list of emails and puts them into a sql database """
#import module
import sqlite3 as sql
#Setup sql
#create connection for sql
connection = sql.connect("emailList.db")
#create cursor
crsr = connection.cursor()
#create sql table
cmd = """CREATE TABLE emails (
email_handle TEXT,
email_domain VARCHAR(20));"""
crsr.execute(cmd)
#iterate through email list
index = 0
for email in email_list:
#split email with a delimiter of "@"
email_list[index] = email.split('@')
index += 1
#while loop to put all data into table
ct = 0
index = 0
while ct <= (len(email_list) - 1):
for i in email_list:
for j in email_list:
email_address_1 = email_list[index]
email_address_2 = email_list[index + 1]
cmd = f"""INSERT INTO emails (email_handle, email_domain)
VALUES ({email_address_1}, {email_address_2});"""
crsr.execite(cmd)
index += 1
ct += 1
#get the contents of the table
crsr.execute("SELECT * FROM emails;")
#store contents in a variable
email_address_list = crsr.fetchall()
#save changes to sql table
connection.commit()
#close connection
connection.close()
#return print statement for data
return print(email_address_list)
I am getting an IndexError:
File "c:/Users/USER/Desktop/email grabber.py", line 82, in
email_address_grab(["[email protected]"])
File "c:/Users/USER/Desktop/email grabber.py", line 57, in email_address_grab
email_address_2 = email_list[index + 1]
IndexError: list index out of range
How can I fix this?
You have 2 loops that iterate on email_list. However you only use your variable index
in your code and you increment it every time.
So let say you have 10 emails in your list. Your double loop will make your code execute 100 times, and the value of index
will continue to grow. After the 11 time your loop is executed, you will try to fetch the 11th value of your email_list
which will produce your error, because you only have 10 value in your list.
What you probably want to do is to use your i, j
variables like this if you want to create every pair possible
email_address_1 = email_list[i]
email_address_2 = email_list[j]
Correct answer by Luke B on January 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP