[英]String Index Out of Range
I am using pandas and pulling information from excel into a list, that part works great and my lists are coming back and I can print them out just fine with no NaN or errors.我正在使用 Pandas 并将信息从 excel 提取到列表中,这部分效果很好,我的列表又回来了,我可以很好地打印出来,没有 NaN 或错误。
However when I go to iterate through them I am encountering an issue with the string index being out of range after the first iteration or when itr is 1 as it prints the first one just fine then errors at the line site = site[itr]
.但是,当我遍历它们时,我遇到了字符串索引在第一次迭代后超出范围的问题,或者当 itr 为 1 时,因为它打印第一个就好了,然后在
site = site[itr]
行出现错误。
The site array or list if you will which is comprised of numeric values.由数值组成的站点数组或列表(如果您愿意)。 I get the count of the number of rows pulled from excel as the index limiter called toSend.
我得到了从 excel 中提取的行数作为称为 toSend 的索引限制器。
I added site = [str(i) for i in site]
because I was getting the error that the int object is not subscriptable.我添加了
site = [str(i) for i in site]
因为我收到了 int 对象不可下标的错误。
I'm not sure where I turned left at, any help is very much appreciated.我不确定我在哪里左转,非常感谢任何帮助。
******* Updated to MRE as requested ******* ******* 根据要求更新为 MRE *******
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import pandas as pd
import time
name = ['Michael Jordan', 'General Clark', 'Tony Tiger', 'Alvin Chipmunk', 'Daffy Duck']
eMail = ['thebulls@bull.com', 'general@insurance.com', 'tiger@post.com', 'alvin@chipmunk.com', 'duck@goose.com']
site = [1, 3, 5, 6, 9]
site = [str(i) for i in site]
toSend = 5
print("Count is " + str(toSend))
print("Starting up... Excel Read")
print(name)
print(site)
print(eMail)
time.sleep(2)
itr = 0
for itr in range(toSend):
print(str(itr))
name = name[itr]
eMail = eMail[itr]
site = site[itr]
print("\nWe will be sending an email statements to " + str(name) + "\nfor site " + str(site) + "\nat email address "+ str(eMail) + "\n")
itr = int(itr) + 1
print("Listing Completed")
Updated:更新:
Its because you are overwriting your variables.这是因为您正在覆盖变量。
So name
starts as an array:所以
name
以数组开头:
name = ['Michael Jordan', 'General Clark', 'Tony Tiger', 'Alvin Chipmunk', 'Daffy Duck']
Loop 1, name
becomes a string (idx = 1)循环1,
name
变成字符串(idx = 1)
name = 'Michael Jordan'
Loop 2, name
becomes a single letter string (idx = 2)循环2,
name
变成单字母字符串(idx = 2)
name = 'i'
Loop 2, name
tries to get position 3 from a single letter string, therefore string index out of range (idx = 3)循环 2,
name
尝试从单个字母字符串中获取位置 3,因此字符串索引超出范围 (idx = 3)
Fixed example:固定示例:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import pandas as pd
import time
name = ['Michael Jordan', 'General Clark', 'Tony Tiger', 'Alvin Chipmunk', 'Daffy Duck']
eMail = ['thebulls@bull.com', 'general@insurance.com', 'tiger@post.com', 'alvin@chipmunk.com', 'duck@goose.com']
site = [1, 3, 5, 6, 9]
site = [str(i) for i in site]
toSend = 5
print("Count is " + str(toSend))
print("Starting up... Excel Read")
print(name)
print(site)
print(eMail)
time.sleep(2)
itr = 0
for itr in range(toSend):
print(str(itr))
loop_name = name[itr]
loop_eMail = eMail[itr]
loop_site = site[itr]
print("\nWe will be sending an email statements to " + str(loop_name) + "\nfor site " + str(loop_site) + "\nat email address "+ str(loop_eMail) + "\n")
print("Listing Completed")
-- Old -- - 老的 -
Just modify your code to a for loop, this will reduce the logic needed and make it easier to debug.只需将代码修改为 for 循环,这将减少所需的逻辑并使调试更容易。
name = sI["Name"].values.tolist()
eMail = sI["MailAddress"].values.tolist()
site = sI["MailSite"].values.tolist()
site = [str(i) for i in site]
toSend = sI["Count"].values
print("Count is " + str(toSend))
print("Starting up... Excel Read")
print(name)
print(site)
print(eMail)
time.sleep(2)
itr = 0
for itr in range(len(toSend)):
print(str(itr))
name = name[itr]
eMail = eMail[itr]
site = site[itr]
print("We will be sending an email statements to " + str(name) + "\nfor site " + str(site) + "\nat email address "+ str(eMail))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.