How do I write my data along the columns in a CSV file?

Viewed 65

When I write to the csv file all of my data is printed in only the first column. Using my loop, how do I iterate along the columns to write the data?

import csv
import bs4
import urllib
from urllib.request import  urlopen as uReq
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup

#For sites that can't be opened due to Urllib blocker, use a Mozilla User agent to get access
pageRequest = Request('https://coronavirusbellcurve.com/', headers = {'User-Agent': 'Mozilla/5.0'})
htmlPage = urlopen(pageRequest).read()
page_soup = soup(htmlPage, 'html.parser')
specificDiv = page_soup.find("div", {"class": "table-responsive-xl"})

TbodyStats = specificDiv.table.tbody.tr.contents
TbodyDates = specificDiv.table.thead.tr.contents


with open('CovidHTML.csv','w', newline= '') as file:
    theWriter = csv.writer(file)             
    theWriter.writerow(['5/4', ' 5/5', ' 5/6',' 5/7',' 5/8',' 5/9'])
    for i in range(3,len(TbodyStats)):
        if i%2 != 0:
            theWriter.writerow([TbodyStats[i].text])
2 Answers

Another method, For reference only.

from simplified_scrapy import SimplifiedDoc,utils,req
html = req.get('https://coronavirusbellcurve.com/')
doc = SimplifiedDoc(html)
specificDiv = doc.select('div.table-responsive-xl') # Get first div. If you want to get all divs, use this method: doc.selects('div.table-responsive-xl')
# TbodyStats = specificDiv.tbody.trs.selects('td|th').text # Get data
# TbodyDates = specificDiv.thead.trs.selects('td|th').text # Get date
data = specificDiv.table.trs.selects('td|th').text # Get all
rows = []
for row in data:
    rows.append(row[1:])
utils.save2csv('test.csv',rows)

Result:

5/5,5/6,5/7,5/8,5/9
1213260,1237960,1266822,1294664,1314610
24423,24700,28862,27842,19946
2.05%,2.04%,2.33%,2.20%,1.54%

I think you may be able to do this (I can't test for sure because I don't have your exact data on hand):

row = []
for i in range(3, len(TbodyStats), 2):
    row.append(TbodyStats[i].text)
    if len(row) == 6:
        theWriter.writerow(row)
        row = []

I added the 'step' to your range so you don't have to use % for finding odd numbered indices, then just built each row until it hits 6 members, then flush that to the csv file, then empty the row so you can repeat the process.

Related