Search code examples
pythonseleniumcsvselenium-webdriverexport-to-csv

Unnecessary commas appearing in Python list


For context, I am quite new to Python and programming in general.

I have a list(created with selenium webdriver)that looks perfectly normal, but when I try to export it to a CSV file, every value in the list is interpreted to have a comma-separated between each string value. This causes the CSV file to create a column for every string value inside my list value.

Within the Numbers macOS app, I changed the settings so that the file "separates values using tab." This puts every list-value under one column(Name), but I still see a comma separating every string value in the CSV file.

My rows now look like this:

B,o,s,t,o,n

But I want it to reflect how it looks in my list on python, which would just be:

Boston

Here is what I have written in Python for writing the CSV file.

from selenium import webdriver
import time
import csv

PATH = "/users/charlesmiele/desktop/chromedriver"
driver = webdriver.Chrome(PATH)
                        

url = 'https://www.niche.com/colleges/search/best-colleges/?page=1'

clist = []

driver.get(url)
time.sleep(1)
data = driver.find_elements_by_tag_name('h2')
for value in data:
        print(value.text)
        clist.append(value.text)
time.sleep(1)

with open("list.csv", 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['Name'])
        writer.writerows(clist)

driver.quit()
print(clist)

Thank you!


Solution

  • You can do this simply with dataframes. It doesn't skip a line in Excel.

    df = pd.DataFrame(clist,columns=['Name'])
    df.to_csv('list.csv', index=False)
    

    Import

    import pandas as pd