![](/img/trans.png)
[英]How to I extract the 2nd element in this numpy array, where the 2nd element of each row is another numpy array
[英]How do i make the data on my 2nd loop save in another row in csv
這是我想要運行的函數,但發生的情況是第二個數據循環替換了保存在 csv 中的第一個數據循環
類搜索():
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)
這是將數據打印到 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()
預期結果 Abad, Christina Q. |通過 Abercrombie, Veronica F. |通過
嘗試以追加模式而不是寫入模式打開您正在寫入的文件。
在您的save_csv
函數中,如果文件已經存在,則with open('test_case.xlsx','w', newline='')...
將覆蓋該文件。 要添加到現有文件中,您需要with open('test_case.xlsx','a', newline='')...
。
參考: https : //docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.