Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python似乎不可避免的索引器错误和UnboundLocalError_Python_Index Error - Fatal编程技术网

Python似乎不可避免的索引器错误和UnboundLocalError

Python似乎不可避免的索引器错误和UnboundLocalError,python,index-error,Python,Index Error,我正在自动化从我的网站将数据导入CSV的过程。回溯可以在下面看到。我收到一个NameError,指出在if/elif条件中未定义mobile和office Traceback (most recent call last): File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search office = emailandphone[1] IndexError: list index out o

我正在自动化从我的网站将数据导入CSV的过程。回溯可以在下面看到。我收到一个NameError,指出在if/elif条件中未定义mobile和office

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1
编辑:假设为空白Book1.csv

from selenium import webdriver
import time
import csv
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("undisclosable")

driver.find_element_by_xpath("//input[@name='rad_region' and @value='far']").click()
time.sleep(2)
driver.find_element_by_xpath("//input[@name='rad_georegion' and @value='east']").click()
time.sleep(2)
driver.find_element_by_xpath("//input[@name='rad_manage' and @value='No']").click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="ModalPopupDiv"]/div/div[3]/div[1]/a').click()

class Search():
    with open(r'Book1.csv', 'r+', encoding='utf-8') as csvfile:
        csvreader = csv.reader(csvfile)
        next(csvreader)
        while True:
                for row in csvreader:
                    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "searchKeyword")))
                    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "go")))
                    driver.find_element_by_id('searchKeyword').clear()
                    time.sleep(1)
                    driver.find_element_by_id('searchKeyword').send_keys(row)
                    time.sleep(1)
                    driver.find_element_by_id('go').click()
                    time.sleep(1)

                    writer = csv.writer(csvfile)
                    tobesplit = str(driver.find_element_by_class_name('snippetContact').text)
                    emailandphone = tobesplit.split("  |  ")
                    try:
                        email = emailandphone[0]
                        office = emailandphone[1]
                        mobile = emailandphone[2]
                        writer.writerow([None, email, office, mobile])
                    except UnboundLocalError:
                        if mobile not in emailandphone:
                            writer.writerow([None, email, office])
                        elif office not in emailandphone:
                            writer.writerow([None, email])
Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1
以下是stacktrace:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“C:/Users/s995648.CS/PycharmProjects/untitled/auto”,第39行,搜索中
office=电子邮件和电话[1]
索引器:列表索引超出范围
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:/Users/s995648.CS/PycharmProjects/untitled/auto”,第19行,在
类搜索():
文件“C:/Users/s995648.CS/PycharmProjects/untitled/auto”,第43行,搜索中
如果手机不在Email和Phone中:
NameError:未定义名称“mobile”
进程已完成,退出代码为1

1>如果您正在解析的数据没有足够的元素用分隔符分隔,那么您将得到下面的错误,它将移动到except块,这意味着移动变量赋值从未发生,因此except块将引发错误。我希望这是清楚的。此外,为了清楚起见,示例中的错误出现得更早,他们建议在尝试结束时使用其他方法,但默认值除外(“无”可能适用于您的用例)

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1

2> 在尝试访问值之前,最好检查列表是否按照您的假设填充,最简单的方法是在赋值后检查每个元素的长度,并相应地进行处理

Hi!欢迎使用Stack Exchange。你能重新表述这个问题以澄清哪些会引起哪些错误吗?你的代码有点不清楚<代码>str未在其中定义,且语法不正确。您可能想咨询一下欢迎使用StackOverflow的必要条件。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。我深表歉意。我已经查看了MCVE并更新了我的一些代码,并将继续这样做。如果我的代码或我的文章没有描述我的问题,我非常抱歉。这正是我的代码作为示例的样子。请随时给我建议,因为我是新的过程。非常感谢。只是想澄清一下其他人所说的:如果(1)有您看到的确切错误,包括堆栈跟踪,以及(2)我可以复制/粘贴到系统上的一些代码,这些代码将运行并产生相同的错误,这将是有帮助的。也许(2)没有那么容易,因为我们没有你的“Book1.csv”文件。
Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1