簡體   English   中英

使用 for 循環寫入 CSV 文件時不保存行

[英]Not saving lines when writing to CSV file using for-loop

所以我正在嘗試運行一個網站。 刮刀運行得很好。 但是,每當我嘗試將抓取的信息/行寫入 csv 文件時,它都會刪除前一行。 我最終只是在最后的文件中獲得了最后的刮擦結果。 我確定這只是一個縮進錯誤? 我還是 Python 的新手,所以任何幫助將不勝感激!

代碼:

# create general field names
fields = ['name', 'about', 'job_title', 'location','company',
          'education','accomplishments','linkedin_url']

with open('ScrapeResults.csv', 'w') as f:
    # using csv.writer method from CSV package
    write = csv.writer(f)
    write.writerow(fields)
f.close()

# Loop-through urls to scrape multiple pages at once
for individual,link in contact_dict.items():

    ## assign ##
    the_name = individual
    the_link = link
    # scrape peoples url:
    person = Person(the_link, driver=driver, close_on_complete=False)

    # rows to be written
    rows = [[person.name, person.about, person.job_title, person.location, person.company,
             person.educations, person.accomplishments, person.linkedin_url]]
    # write
    with open('ScrapeResults.csv', 'w') as f:
    # using csv.writer method from CSV package
        write = csv.writer(f)
        write.writerows(rows)
        f.close()

您需要在append模式下打開文件。

改變

with open('ScrapeResults.csv', 'w') as f:

with open('ScrapeResults.csv', 'a') as f:

我認為您正在覆蓋文件而不是附加它。 for 循環中的 Append 模式應該可以解決問題

with open('ScrapeResults.csv', "a")

請參考 python IO 文檔: https://docs.Z23EEEB4347BDD26BFC6B7EE3/tutorial-filesoutput.html#Z-and-tutorial-filesoutput.html#Z.org

您必須以append 模式打開文件。

代替

with open('ScrapeResults.csv', 'w') as f:

with open('ScrapeResults.csv', 'a') as f:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM