如何创建遍历多个数组并将它们写入 csv 的 for 循环?



我正在尝试编写一个脚本来抓取包含四列的网页,将每列的内容分配给一个数组,最后我想创建一个 for 循环,将每个数组的i'th元素写入 csv 中各自的列中。

我遇到的问题是我不知道如何创建一个可以做到这一点的 for 循环。我已经写了f.write()函数。我希望电子表格看起来像这样:

https://i.stack.imgur.com/NSrbS.jpg

这是我到目前为止的代码。请记住,我仍然是初学者:

from urllib.request import urlopen
from bs4 import BeautifulSoup
my_url = input()
uClient = urlopen(my_url)
page_css = uClient.read()
uClient.close()
page_soup = BeautifulSoup(page_css, "html.parser")
filename = "ge_scrape"
f = open(filename, "w")
headers ="Tag, , Name, , VR, , Valuen"
f.write(headers)
#The following for loops assign values from the columns 'tag', 'name', 'vr', and 'value' to an empty array
#so you can iterate over the arrays and print them in a .csv with a for loop that contains f.write().
tag_array = []
tag_containers = page_soup.findAll("td",{"id":"tag"})
for container in tag_containers:
tag = container.get_text()
tag_array.append(tag)
name_array =  []
name_containers = page_soup.findAll("td",{"id":"name"})
for container in name_containers:
name = container.get_text()
name_array.append(name)
vr_array =  []
vr_containers = page_soup.findAll("td",{"id":"vr"})
for container in vr_containers:
vr = container.get_text()
vr_array.append(vr)
val_array =  []
val_container = page_soup.findAll("td",{"id":"val"})
for container in val_container:
val = container.get_text()
val_array.append(val)
#Below is the attempted code:
i = 0
for (tag, name, vr, val) in zip(tag_array, name_array, vr_array, val_array):
f.write(tag_array[i] + "," + name_array[i] + "," + vr_array[i] + "," + val_array[i] + "n")
i = i + 1

如果数组/列表的长度相等,则可以使用 zip 组合列表并返回元组的迭代器。如果列表长度不同,您可以使用 itertools zip_longest并提供缺失的默认值。

import csv
list_A = [1,2,3,4]
list_B = ['a','b','c','d']
list_C = ['A','B','C','D']
list_D = [99,999,9999,99999]
with open("data.csv", "w", encoding="utf-8-sig", newline='') as csv_file:
w = csv.writer(csv_file, delimiter = ",", quoting=csv.QUOTE_MINIMAL)
w.writerow(['Col1','Col2','Col3','Col4'])
for row in zip(list_A,list_B,list_C,list_D):
w.writerow(row)

最新更新