Python csv writer 在每个字母后输出逗号
Python csv writer outputs commas after each letter
我的 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)
all_data.append(business_data)
将一个字符串附加到 all_data,
所以 csv_writer.writerow(line)
将 line
作为字符串,而它期望 list
。
尝试:
for line in all_data:
array = line.split(',')
csv_writer.writerow(array)
print(line)
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])
我的 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)
all_data.append(business_data)
将一个字符串附加到 all_data,
所以 csv_writer.writerow(line)
将 line
作为字符串,而它期望 list
。
尝试:
for line in all_data:
array = line.split(',')
csv_writer.writerow(array)
print(line)
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])