由于某种原因,Python CSV为每个字符写一列

时间:2016-02-19 01:37:29

标签: python

我不断获得一个输出(下图),它在excel中每列写一个字符。我做错了什么?

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))

1 个答案:

答案 0 :(得分:1)

您需要将列值列表传递到writerow(),但目前您正在传递字符串。替换:

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

使用:

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