How to create two columns in an csv file from an for loop in python?

import json
from io import StringIO
from bs4 import BeautifulSoup
from requests_html import HTMLSession
import time
from selenium import webdriver
import requests
import pandas as pd
import numpy as np
from selenium.webdriver.chrome.options import Options
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset = False)
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PATH = "C:Program Files (x86)chromedriver.exe"
chrome_options = Options()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(PATH, options = chrome_options)
driver.minimize_window()

for b in range(500):

    driver.implicitly_wait(10)
    url = "https://www.healthgrades.com/usearch?what=Marriage%20%26%20Family%20Therapy&entityCode=PS303&where=CA&pageNum={}&sort.provider=bestmatch&state=CA".format(b+104)
    driver.get(url)
    time.sleep(10)
    length = len(driver.find_elements_by_xpath("//a[@data-qa-target='provider-details-provider-name']"))
    for i in range(length):
        elements = driver.find_elements_by_xpath("//a[@data-qa-target='provider-details-provider-name']")
        elements[i].click()
        handles = driver.window_handles
        driver.switch_to.window(handles[1])
        time.sleep(1)
        print(driver.find_element_by_tag_name("h1").get_attribute("innerText"))
        print(driver.find_element_by_xpath("//div[@class='summary-standard-button-row-mobile']/a").get_attribute("innerText"))
        driver.close()
        driver.switch_to.window(handles[0])
        time.sleep(1)

The output of this code is like this:

Full Name
Phone Number

Is there a way so I can import the name and phone number in separate columns in a CSV file?

I’ve tried numerous ways but they sadly do not work because the for loop rewrites my rows in the CSV file.

Answer

Something like this.

import csv

# ...

with open('phone.csv' 'w') as file:
    writer = csv.writer(file)
    for b in range(500):
        # ...
        for i in range(length):
            # ...
            name = driver.find_element_by_tag_name("h1").get_attribute("innerText")
            phone = driver.find_element_by_xpath("//div[@class='summary-standard-button-row-mobile']/a").get_attribute("innerText")
            writer.writerow([name, phone])