Python csv编写器在每个字母后输出逗号

时间:2018-08-21 16:41:34

标签: python csv writer

我的csv文件除了每个字母后都带有逗号。它将数据写入换行符。我在做什么数据错误?在将每个地址放入列表后,我尝试将每个地址写入csv中的新行。我尝试了各种不同的方法将数据写入csv文件,但是没有任何效果。我不知道该怎么做才能使数据写入名称,而不会出现很多逗号。

import requests
from bs4 import BeautifulSoup
import csv

search_terms = "Bars"
location = "New Orleans, LA"
all_data = []

if ' ' in search_terms:
    search_terms = search_terms.replace(' ', '+')
    print(search_terms)
if ', ' in location:
    location = location.replace(', ', '+')
    print(location)
count = 1

while True:
    page_number = str(count)
    url = "https://www.yellowpages.com/search? search_terms="+search_terms+"&geo_location_terms="+location+"&page="+page_number
    print(url)
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')
    info = soup.findAll("div", {"class": "info"})
    if soup.findAll("div", {"class": "info"}):
        count = count+1
        for each in info:
            try:
                business_name = each.find(itemprop="name").get_text()
            except:
                business_name = "No Business Name"
            try:
                street = each.find(itemprop="streetAddress").get_text()
            except:
                street = "No Street Address"
            try:
                city = each.find(itemprop="addressLocality").get_text()
            except:
                city = "No City,"
            try:
                state = each.find(itemprop="addressRegion").get_text()
            except:
                state = "No State"
            try:
                zip = each.find(itemprop="postalCode").get_text()
            except:
                zip = "No Zip Code"
            try:
                telephone = each.find(itemprop="telephone").get_text()
            except:
                telephone = "No Telephone"
            business_data = business_name+","+street+","+city+state+","+zip+","+telephone
            business_data = business_data.replace(u'\xa0', u'')
            all_data.append(business_data)
    else:
        break
with open(search_terms+'.csv', 'w+') as wf:
    csv_writer = csv.writer(wf)
    csv_writer.writerow(["Business Name","Street Address", "City", "State", "Zip", "Telephone"])
    for line in all_data:
        csv_writer.writerow(line)
        print(line)

2 个答案:

答案 0 :(得分:1)

END {print p} 将一个字符串附加到all_data, 因此all_data.append(business_data)csv_writer.writerow(line)作为字符串,而期望line。 试试:

list

答案 1 :(得分:1)

writerow方法期望由该行的所有项目组成的可迭代对象。在向其传递字符串时,它将迭代每个字符并将每个字符视为一个项目。

要遍历字段,而不是

business_data = business_name+","+street+","+city+state+","+zip+","+telephone
all_data.append(business_data)

您可能想尝试类似的东西

all_data.append([business_name, street, city + state, zip, telephone])