简体   繁体   English

写入 CSV 文件并使用 python 添加列?

[英]Writing to a CSV file and adding columns using python?

So I've never really used import csv before, but I've managed to scrape a bunch of information from websites and now want to put them in a csv file.所以我以前从来没有真正使用过 import csv ,但我已经设法从网站上抓取了一堆信息,现在想把它们放在 csv 文件中。 The issue I'm having is that all my list values are being separated by commas (ie Jane Doe = J,a,n,e, ,D,o,e).我遇到的问题是我所有的列表值都用逗号分隔(即 Jane Doe = J,a,n,e, ,D,o,e)。

Also, I have three lists (one with names, one with emails, and one with titles) and I would like to add them each as its own column in the CSV file (so col1 = Name, col2 = title, col3= email)另外,我有三个列表(一个带有名称,一个带有电子邮件,一个带有标题),我想将它们分别添加为 CSV 文件中的自己的列(因此 col1 = 名称,col2 = 标题,col3 = 电子邮件)

Any thoughts on how to execute this?关于如何执行此操作的任何想法? Thanks.谢谢。


from bs4 import BeautifulSoup
import requests
import csv


urls = ''

with open('websites.txt', 'r') as f:
    for line in f.read():
        urls += line

urls = list(urls.split())

name_lst = []
position_lst = []
email_lst = []

for url in urls:

    print(f'CURRENTLY PARSING: {url}')
    print()

    res = requests.get(url)
    soup = BeautifulSoup(res.text, 'html.parser')

    try:
        for information in soup.find_all('tr', class_='sidearm-staff-member'):
            names = information.find("th", attrs={'headers': "col-fullname"}).text.strip()
            positions = information.find("td", attrs={'headers': "col-staff_title"}).text.strip()
            emails = information.find("td", attrs={'headers': "col-staff_email"}).script
            target = emails.text.split('var firstHalf = "')[1]
            fh = target.split('";')[0]
            lh = target.split('var secondHalf = "')[1].split('";')[0]
            emails = fh + '@' + lh

            name_lst.append(names)
            position_lst.append(positions)
            email_lst.append(emails)


    except Exception as e:
        pass
       
with open('test.csv', 'w') as csv_file:
    csv_writer = csv.writer(csv_file)
    for line in name_lst:
        csv_writer.writerow(line)
    for line in position_lst:
        csv_writer.writerow(line)
    for line in email_lst:
        csv_writer.writerow(line)

Writing your data column-by-column is easy.逐列写入数据很容易。 All you have to do is write the rows where each row contains elements of the 3 tables with the same list index.您所要做的就是编写每行包含具有相同列表索引的 3 个表的元素的行。 Here is the code:这是代码:

with open('test.csv', 'w') as csv_file:
    csv_writer = csv.writer(csv_file)
    for name, position, email in zip(name_lst, position_lst, email_lst):
        csv_writer.writerow([name, position, email])

Assuming that the name_lst, position_lst and email_lst are all correct and are of the same size, Your problem is in the last part of your code where you write it to a CSV file.假设 name_lst、position_lst 和 email_lst 都是正确的并且大小相同,您的问题出在代码的最后一部分,您将其写入 CSV 文件。

Here is a way to do this:这是一种方法:

fieldnames = ['Name', 'Position', 'Email']
with open('Data_to_Csv.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for i in range(len(name_lst)):
        writer.writerow({'Name':name_lst[i],'Position':position_lst[i], 'Email':email_lst[i]})

This would of course fail if you are the length of the lists are unequal.如果列表的长度不相等,这当然会失败。 You need to make sure that you are adding dummy values for entries that are not available to make sure that 3 lists have equal number of values.您需要确保为不可用的条目添加虚拟值,以确保 3 个列表具有相同数量的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM