写入csv文件python的第二列
我有一个Python程序,在从CSV文件(只有一列)中读取餐厅名称后,它可以获得谷歌对餐厅的评级。名称在一列中(第一列)。它获取每个餐厅的评级并将其添加到列表中。现在我需要创建第二列,并将该列表传递到第二列。这是我的CSV文件(它没有标题,即列的名称) 代码如下:写入csv文件python的第二列,python,pandas,csv,Python,Pandas,Csv,我有一个Python程序,在从CSV文件(只有一列)中读取餐厅名称后,它可以获得谷歌对餐厅的评级。名称在一列中(第一列)。它获取每个餐厅的评级并将其添加到列表中。现在我需要创建第二列,并将该列表传递到第二列。这是我的CSV文件(它没有标题,即列的名称) 代码如下: import requests from bs4 import BeautifulSoup import csv def main(): global rate ratings = [] res = []
import requests
from bs4 import BeautifulSoup
import csv
def main():
global rate
ratings = []
res = []
with open('CSV restaurants example.csv', newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row1 in reader:
for row in row1:
res.append(row)
for restaurant in res:
try:
re = requests.get('https://www.google.com/search?q=' + restaurant)
except requests.exceptions.ChunkedEncodingError:
re = requests.get('https://www.google.com/search?q=' + restaurant)
soup = BeautifulSoup(re.content, 'html.parser')
rating = soup.select_one('.oqSTJd')
try:
rate = rating.text
ratings.append(rate)
except AttributeError:
print("google search is asking for captcha, use a proxy or change your wifi network")
exit()
print('\nThe rating for ' + restaurant + ' is ' + str(rate) + ' out of 5 ' + '\n')
if __name__ == '__main__':
main()
关于如何将该列表附加到第二列有何建议?试试这个,它会创建一个带有第二列评级的熊猫对象,您可以将其转换为csv:
import requests
import pandas as pd
from bs4 import BeautifulSoup
import csv
def main():
global rate
ratings = []
res = []
with open('CSV restaurants example.csv', newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row1 in reader:
for row in row1:
res.append(row)
for restaurant in res:
try:
re = requests.get('https://www.google.com/search?q=' + restaurant)
except requests.exceptions.ChunkedEncodingError:
re = requests.get('https://www.google.com/search?q=' + restaurant)
soup = BeautifulSoup(re.content, 'html.parser')
rating = soup.select_one('.oqSTJd')
try:
rate = rating.text
ratings.append(rate)
except AttributeError:
print("google search is asking for captcha, use a proxy or change your wifi network")
exit()
print('\nThe rating for ' + restaurant + ' is ' + str(rate) + ' out of 5 ' + '\n')
df2 = pd.read_csv('CSV restaurants example.csv', header=None, names=['Restaurants'])
df2['ratings'] = ratings
print(df2)
print("")
print(df2.to_csv())
return df2
if __name__ == '__main__':
df2 = main()
您可以重写回csv,而不使用如下标题:
df2.set_index("Restaurants").to_csv(header=False)
# 'Barrafina london,4.5\npalomar london,4.6\nfateema london,4.8\nfive guys london,4.2\n10 greek street london,4.5\nedition hotel london,4.6\n'
只需创建一个
csv.writer
来赞美您的csv.reader
。如果要将输出保存为新文件,我建议永远不要覆盖原始数据,即使只是向其中添加数据
我要说的是,您可能需要小心分隔符,因为位置和餐厅字段似乎合并了,在我看来,这就像是格式错误的csv中的两列
在这里,您可以在相同的for循环中解决它:
import requests
from bs4 import BeautifulSoup
import csv
def main():
# global rate
# ratings = []
# res = []
with open('CSV restaurants example.csv', newline='', encoding='utf-8') as infd, \
open('restaurants_with_ratings.csv') as outfd:
reader = csv.reader(infd)
# create a csv writer, these are defaults, but I'm showing that
# you can choose formatting
writer = csv.writer(outfd, delimiter=',', quotechar='"')
# if there's only one column, the comma works here.
for restaurant, in reader:
# res.append(row)
try:
re = requests.get('https://www.google.com/search?q=' + restaurant)
except requests.exceptions.ChunkedEncodingError:
re = requests.get('https://www.google.com/search?q=' + restaurant)
soup = BeautifulSoup(re.content, 'html.parser')
rating = soup.select_one('.oqSTJd')
try:
rate = rating.text
writer.writerow([restauant, rate]) # add a column
# ratings.append(rate)
except AttributeError:
print("google search is asking for captcha, use a proxy or change your wifi network")
exit()
print('\nThe rating for ' + restaurant + ' is ' + str(rate) + ' out of 5 ' + '\n')
if __name__ == '__main__':
main()
import requests
from bs4 import BeautifulSoup
import csv
def main():
# global rate
# ratings = []
# res = []
with open('CSV restaurants example.csv', newline='', encoding='utf-8') as infd, \
open('restaurants_with_ratings.csv') as outfd:
reader = csv.reader(infd)
# create a csv writer, these are defaults, but I'm showing that
# you can choose formatting
writer = csv.writer(outfd, delimiter=',', quotechar='"')
# if there's only one column, the comma works here.
for restaurant, in reader:
# res.append(row)
try:
re = requests.get('https://www.google.com/search?q=' + restaurant)
except requests.exceptions.ChunkedEncodingError:
re = requests.get('https://www.google.com/search?q=' + restaurant)
soup = BeautifulSoup(re.content, 'html.parser')
rating = soup.select_one('.oqSTJd')
try:
rate = rating.text
writer.writerow([restauant, rate]) # add a column
# ratings.append(rate)
except AttributeError:
print("google search is asking for captcha, use a proxy or change your wifi network")
exit()
print('\nThe rating for ' + restaurant + ' is ' + str(rate) + ' out of 5 ' + '\n')
if __name__ == '__main__':
main()