如何将列表放入 csv 文件 python 中的一个单元格(在我的例子中是一本书的作者列表),所有作者都在一个单元格中
How to get list into ONE cell in csv file python ( in my case list of authors of a book ), all authors in one cell
我正在尝试从图书网站获取图书作者的姓名。我在一列但多行中得到名称。我想要 csv 的一个单元格中的所有名称。以下是我的完整代码
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome()
site = 'https://www.goodreads.com/book/show/50148349-manto-and-chughtai?from_search=true&from_srp=true&qid=ZARMElvyyt&rank=3'
driver.get(site)
authors = [ ]
names = driver.find_elements_by_xpath('//div[@class="authorName__container"]')
for name in names:
authors.append(name.find_element_by_xpath('.//a[@class="authorName"]').text)
df = pd.DataFrame({'Author Names': authors})
df.to_csv("Authors_list.csv", index=False)
print(df)
这是我的输出,我得到了,我想在一个单元格中显示所有这四个名字
我没有安装和设置 Selenium 来测试它。
你能试试这个小调整吗?
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome()
site = 'https://www.goodreads.com/book/show/50148349-manto-and-chughtai?from_search=true&from_srp=true&qid=ZARMElvyyt&rank=3'
driver.get(site)
authors = [ ]
names = driver.find_elements_by_xpath('//div[@class="authorName__container"]')
for name in names:
authors.append(name.find_element_by_xpath('.//a[@class="authorName"]').text)
authors_in_one_cell = ', '.join(authors)
df = pd.DataFrame({'Author Names': authors_in_one_cell})
df.to_csv("Authors_list.csv", index=False)
print(df)
Pandas dataframe.to_csv() method will write each list element with a newline from the looks of it. You're unintentionally accomplishing what this stack overflow question was .
尝试将 authors 设为一个字符串,并将每个新作者附加到该字符串而不是一个列表。只要作者姓名中没有逗号,在办公室打开后所有值都会出现在同一个单元格中。
你可以试试这个。
authors = ','.join(df['authors'].to_list())
with open('mycsv.csv', 'w', newline='') as myfile:
myfile.write(authors)
我正在尝试从图书网站获取图书作者的姓名。我在一列但多行中得到名称。我想要 csv 的一个单元格中的所有名称。以下是我的完整代码
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome()
site = 'https://www.goodreads.com/book/show/50148349-manto-and-chughtai?from_search=true&from_srp=true&qid=ZARMElvyyt&rank=3'
driver.get(site)
authors = [ ]
names = driver.find_elements_by_xpath('//div[@class="authorName__container"]')
for name in names:
authors.append(name.find_element_by_xpath('.//a[@class="authorName"]').text)
df = pd.DataFrame({'Author Names': authors})
df.to_csv("Authors_list.csv", index=False)
print(df)
这是我的输出,我得到了,我想在一个单元格中显示所有这四个名字
我没有安装和设置 Selenium 来测试它。
你能试试这个小调整吗?
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome()
site = 'https://www.goodreads.com/book/show/50148349-manto-and-chughtai?from_search=true&from_srp=true&qid=ZARMElvyyt&rank=3'
driver.get(site)
authors = [ ]
names = driver.find_elements_by_xpath('//div[@class="authorName__container"]')
for name in names:
authors.append(name.find_element_by_xpath('.//a[@class="authorName"]').text)
authors_in_one_cell = ', '.join(authors)
df = pd.DataFrame({'Author Names': authors_in_one_cell})
df.to_csv("Authors_list.csv", index=False)
print(df)
Pandas dataframe.to_csv() method will write each list element with a newline from the looks of it. You're unintentionally accomplishing what this stack overflow question was
尝试将 authors 设为一个字符串,并将每个新作者附加到该字符串而不是一个列表。只要作者姓名中没有逗号,在办公室打开后所有值都会出现在同一个单元格中。
你可以试试这个。
authors = ','.join(df['authors'].to_list())
with open('mycsv.csv', 'w', newline='') as myfile:
myfile.write(authors)