[英]Writing python list values to a csv file
我正在嘗試將生成的列表導出到csv文件,其中網站表中的每一行對應於文件中的新行,並且每個值都在單個單元格中,例如:
NAME ..... ICO DATE .... ICO PRICE .... CURR。 價格...... 24 HR HRI Stratis ..... 06/20/16 ....... $ 0.007 ........... $ 7.480 ........... ..... + 38.80%
當前輸出如下所示:
[ 'Patientory \\ N05 /一十七分之三十一\\ n $的0.104 \\ N $ 0.274 \\ N + 46.11%\\ N + 25.54%\\ NN / A']
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
csvrows = []
def get_css_sel(selector):
posts = browser.find_elements_by_css_selector(selector)
for post in posts:
print(post.text)
csvrows.append([post.text])
browser = webdriver.Chrome(executable_path=r'C:\Scrapers\chromedriver.exe')
browser.get("https://icostats.com")
wait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div:nth-child(2) > div:nth-child(8)")))
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tableheader-0-50") #fetch header of table
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div") #fetch rows of table
def create_csv(thelist):
with open('ICO.csv', 'w') as myfile:
for i in thelist:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow([i])
create_csv(csvrows)
在get_css_sel()
,每個post.text
包含由換行符分隔的行文本\\n
- 與輸出示例相同。 因此,附加[post.text]
附加一個列表,其中包含整行的單個項目。 改為:
csvrows.append(post.text.split('\n')) # remove the extra list brackets
# since split returns a list.
例如:
>>> y = 'Patientory\n05/31/17\n$0.104\n$0.274\n+46.11%\n+25.54%\nN/A'
>>> y.split('\n')
['Patientory', '05/31/17', '$0.104', '$0.274', '+46.11%', '+25.54%', 'N/A']
另外,在你的寫入循環中,你不應該為每一行重新創建csv.writer
,只需在循環遍歷thelist
之前執行一次。
既然你在csvrows
擁有了你想要的所有行,你可以直接使用csvwriter.writerows
。
def create_csv(thelist):
with open('ICO.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(thelist)
試試這段代碼:
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
csvrows = []
def get_css_sel(selector):
posts = browser.find_elements_by_css_selector(selector)
for post in posts:
print(post.text)
csvrows.append(post.text)
browser = webdriver.Chrome(executable_path=r'//Users/Pranavtadepalli/Downloads/chromedriver')
browser.get("https://icostats.com")
wait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div:nth-child(2) > div:nth-child(8)")))
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tableheader-0-50") #fetch header of table
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div") #fetch rows of table
new=[",".join(elem.split("\n")) for elem in csvrows]
newfile=open("csvfile.csv",'r')
newfile1=open("csvfile.csv",'w')
newstuff=newfile.read()
for elem in new:
newfile1.write(elem+'\n')
newfile1.close()
newfile.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.