我已经在笔记本上正常打印了,但是我不明白为什么它会在excel文件中显示不必要的文本。这是代码段。
file = open('targeted_prods.csv', 'w')
writer = csv.writer(file)
writer.writerow(['Product Name', 'Brand', 'Price'])
for prod_info in prod_prev:
prod_name = prod_info.find('div', {'class': 'product-name'}).h3.a.attrs['title'].text
brand_name = prod_info.find('div', {'class': 'product-name'}).find('h4').text
price = prod_info.find('div', {'class': 'product-price'}).find('span').text
print(prod_name + ' ' + brand_name + ' ' + price)
writer.writerow([prod_name.encode('utf-8'), brand_name.encode('utf-8'), price.encode('utf-8')])
Excel文件: