James
James

Reputation: 55

Python CSV writing one column per character for some reason

I keep getting an output (image below) which writes one character per column in excel. What am I doing wrong?

Output screenshot

r= requests.get(url)
soup = BeautifulSoup(r.content)

listing_title= soup.find_all("div",{"class":"listingTitle"})

car_info_data =  soup.find_all("li",{"class": "titleCol"})

with open('testinggggg.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    for item in car_info_data:     #Car name ;
        print(str(item.contents[1].text))
        writer.writerow(str(item.contents[1].text))

Upvotes: 1

Views: 1067

Answers (1)

alecxe
alecxe

Reputation: 474191

You need to pass a list of column values into the writerow(), but currently you are passing a string. Replace:

writer.writerow(str(item.contents[1].text))

with:

writer.writerow([str(item.contents[1].text)])

Upvotes: 1

Related Questions