Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将字符串写入csv中的一个单元格?_Python_Excel_Csv_Python Requests - Fatal编程技术网

如何使用Python将字符串写入csv中的一个单元格?

如何使用Python将字符串写入csv中的一个单元格?,python,excel,csv,python-requests,Python,Excel,Csv,Python Requests,我试图使用Python中的request和Beautiful Soup 4从Zomato中的一个页面中提取评论。我想将请求页面的链接和提取的评论存储到一个csv文件中 我的问题是,我提取的评论没有存储到一个单元格中,而是拆分为多个单元格。如何将提取的评论存储到一个单元格中 这是我的密码: import time from bs4 import BeautifulSoup import requests URL = "https://www.zomato.com/review/eQEygl" t

我试图使用Python中的request和Beautiful Soup 4从Zomato中的一个页面中提取评论。我想将请求页面的链接和提取的评论存储到一个csv文件中

我的问题是,我提取的评论没有存储到一个单元格中,而是拆分为多个单元格。如何将提取的评论存储到一个单元格中

这是我的密码:

import time
from bs4 import BeautifulSoup
import requests

URL = "https://www.zomato.com/review/eQEygl"
time.sleep(2)
reviewPage = requests.get(URL, headers = {'user-agent': 'my-app/0.0.1'})
reviewSoup = BeautifulSoup(reviewPage.content,"html.parser")
reviewText = reviewSoup.find("div",{"class":"rev-text"})
textSoup = BeautifulSoup(str(reviewText), "html.parser")

reviewElem = [URL, ""]
for string in textSoup.stripped_strings:
        reviewElem[1] += string

csv = open("out.csv", "w", encoding="utf-8")
csv.write("Link, Review\n")
row = reviewElem[0] + "," + reviewElem[1] + "\n"
csv.write(row)

csv.close()

无需手动构造CSV字符串。手动执行此操作时,如果列值中有列分隔符(
,默认情况下为
),它们将被解释为分隔符,而不是导致列值分散在多个列中的文字字符串

使用
csv
模块和方法:


我认为问题在于
reviewElem[1]
字符串中嵌入的逗号,因为它们是大多数CSV软件中的默认分隔符。以下内容通过将字符串的内容包装为
字符以指示其全部为一个单元格来避免问题:

import time
from bs4 import BeautifulSoup
import requests

URL = "https://www.zomato.com/review/eQEygl"
time.sleep(2)
reviewPage = requests.get(URL, headers = {'user-agent': 'my-app/0.0.1'})
reviewSoup = BeautifulSoup(reviewPage.content,"html.parser")
reviewText = reviewSoup.find("div",{"class":"rev-text"})
textSoup = BeautifulSoup(str(reviewText), "html.parser")

reviewElem = [URL, ""]
for string in textSoup.stripped_strings:
    reviewElem[1] += string

csv = open("out.csv", "w", encoding="utf-8")
csv.write("Link, Review\n")
#row = reviewElem[0] + "," + reviewElem[1] + "\n"
row = reviewElem[0] + ',"{}"\n'.format(reviewElem[1])  # quote string 2
csv.write(row)

csv.close()

我得到一个类似这样的错误:“TypeError:“fieldnames”是此函数的无效关键字参数“使用你的代码。@justinmanalil好的,让我们使用
writerow
两种方法,尝试一下。遗憾的是,你实际上遵循的是手动解决方法,而不是可靠的Pythonic解决方案。让我们看看当评论包含双引号时会发生什么。
import time
from bs4 import BeautifulSoup
import requests

URL = "https://www.zomato.com/review/eQEygl"
time.sleep(2)
reviewPage = requests.get(URL, headers = {'user-agent': 'my-app/0.0.1'})
reviewSoup = BeautifulSoup(reviewPage.content,"html.parser")
reviewText = reviewSoup.find("div",{"class":"rev-text"})
textSoup = BeautifulSoup(str(reviewText), "html.parser")

reviewElem = [URL, ""]
for string in textSoup.stripped_strings:
    reviewElem[1] += string

csv = open("out.csv", "w", encoding="utf-8")
csv.write("Link, Review\n")
#row = reviewElem[0] + "," + reviewElem[1] + "\n"
row = reviewElem[0] + ',"{}"\n'.format(reviewElem[1])  # quote string 2
csv.write(row)

csv.close()