简体   繁体   English

我如何将第二个循环中的数据保存在 csv 的另一行中

[英]How do i make the data on my 2nd loop save in another row in csv

This is the function that i want to run but what happened is that the 2nd loop of data replaces the first loop of data that was saved in the csv这是我想要运行的函数,但发生的情况是第二个数据循环替换了保存在 csv 中的第一个数据循环
class Search():类搜索():

def searchtest(self):

    # print(soup)

    test_words = ['abad','abercrombie']
    full_name = ['Abad, Christina Q.','Abercrombie, Veronica F.']
    test_word = ['STEVEN']
    x = 0



    for i in test_words:
            print("------------")  
            search = self.driver.find_element_by_xpath('//*[@id="page-wrapper"]/div[3]/div/div/div/div/div[3]/tabletoolstrans/div/input')
            search.clear()
            search.send_keys(i)
            search.send_keys(Keys.RETURN)
            time.sleep(3)
            soup = BeautifulSoup(self.driver.page_source,"html.parser")
            for item in soup.findAll("tr", {"class": "hand_cursor ng-scope"}):
                for td in item.findAll("td")[1]:
                        name = full_name[x]
                        print(name)
                        if td == name:
                            print("Search :"+name+"")
                            print("Pass")
                            buttons.save_csv(self, name, "Pass")
                        else:
                            print("Search :"+name+"")
                            buttons.save_csv(self, name, "Fail")
            x += 1
            print("<------------>")
    time.sleep(3)

This is the csv function that prints the data into the excel def save_csv(self, name, a):这是将数据打印到 excel def save_csv(self, name, a) 的 csv 函数:

    with open('test_case.xlsx','w', newline='') as csvfile:
        obj = csv.writer(csvfile, delimiter='\t',)
        obj.writerow([name,a])

    csvfile.close()

Expected Result Abad, Christina Q. |Pass Abercrombie, Veronica F. |Pass预期结果 Abad, Christina Q. |通过 Abercrombie, Veronica F. |通过

Try opening the file you're writing to in append mode rather than write mode.尝试以追加模式而不是写入模式打开您正在写入的文件。

In your save_csv function, with open('test_case.xlsx','w', newline='')... will overwrite the file if it already exists.在您的save_csv函数中,如果文件已经存在,则with open('test_case.xlsx','w', newline='')...将覆盖该文件。 To add onto an existing file, you want with open('test_case.xlsx','a', newline='')... .要添加到现有文件中,您需要with open('test_case.xlsx','a', newline='')...

Reference: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files参考: https : //docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提取此numpy数组中的第二个元素,其中每行的第二个元素是另一个numpy数组 - How to I extract the 2nd element in this numpy array, where the 2nd element of each row is another numpy array Python CSV跳过或删除第2行 - Python CSV skip or delete 2nd row 我有 2 个 random.randint 值,如何使第二个出现? - I have 2 random.randint values, how do I make the 2nd one come up? 如何反向遍历地图对象的2ND值? - How do I loop over a map object's 2ND value in reverse? 如何制作对角矩阵的掩码,但从第二列开始? - How do I make a mask of diagonal matrix, but starting from the 2nd column? 如何确定是否选择了列表中的第 2 项或第 1 项? - How do I make a condition to find out if the 2nd or 1st item in a list was selected? 如何在pandas中每天提取第二行和第五行数据? - how to extract 2nd and fifth row of data in everyday in pandas? 为什么我的循环第二次运行时出现错误? TypeError:“ int”对象没有属性“ __getitem__” - Why do I get and error when my loop runs the 2nd time? TypeError: 'int' object has no attribute '__getitem__' 如何做到这一点,当我将一行插入 csv 时,它会将其添加到另一行 - How do I make it so when I insert a row to csv it adds it to another line 如何在Python中将第一行的结束时间作为第二行的开始时间 - How to make end time of 1st row as start time of 2nd row in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM