[英]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.