2
我试图将列表的内容添加到CSV文件。首先,我使用BeautifulSoup来抓取第一列内容的网页。然后我再次使用BeautifulSoup来清除其余列的内容。我的代码是:将数据列表添加到CSV文件的单个单元格
# Content first column
playerName = soup.find('div', class_='font-16 fh-red').text
# Content second column
playerStats = []
for stat in soup.find_all('span', class_='player-stat-value'):
playerStats.append(stat.text)
# Write name and stats to CSV file
with open('players.csv', 'a') as csvfile:
dataWriter = csv.writer(csvfile)
dataWriter.writerow([playerName, playerStats])
playerName被正确写入CSV文件。但是,整个playerStats列表被写入第二列。 我希望将各个列表元素写入CSV文件的第二,第三,第四等列。我该怎么做?
只是为了澄清:我在'a'模式下打开文件,因为我之前在Python代码中编写了CSV文件的头文件。
工作就像一个魅力,非常感谢! –