Python 将结果写入同一Excel文件中的两个不同工作表

Python 将结果写入同一Excel文件中的两个不同工作表,python,xlwt,Python,Xlwt,您能教我Python是否可以写入同一个Excel文件,但可以写入两个不同的电子表格(制表符)吗 举个例子,我想选择并编写以下4个网站的标题,并将它们写入同一个文件title.xls,但分别在其Sheet1和Sheet2中 www.dailynews.com www.dailynews.co.zw www.gulf-daily-news.com www.dailynews.gov.bw 我在两个脚本中执行这些操作,每个脚本用于两个网站: from bs4 import BeautifulSoup

您能教我Python是否可以写入同一个Excel文件,但可以写入两个不同的电子表格(制表符)吗

举个例子,我想选择并编写以下4个网站的标题,并将它们写入同一个文件title.xls,但分别在其Sheet1和Sheet2中

www.dailynews.com
www.dailynews.co.zw
www.gulf-daily-news.com
www.dailynews.gov.bw
我在两个脚本中执行这些操作,每个脚本用于两个网站:

from bs4 import BeautifulSoup
import urllib2
import xlwt

line_in_list = ['www.dailynews.com','www.dailynews.co.zw'] 
# line_in_list = [www.gulf-daily-news.com','www.dailynews.gov.bw']

book = xlwt.Workbook(encoding='utf-8', style_compression = 0)
sheet = book.add_sheet('Sheet1', cell_overwrite_ok = True) 
# sheet = book.add_sheet('Sheet2', cell_overwrite_ok = True)

for cor,websites in enumerate(line_in_list):
    url = "http://" + websites
    page = urllib2.urlopen(url)
    soup = BeautifulSoup(page.read())
    site_title = soup.find_all("title")
    print site_title
    sheet.write (cor, 0, site_title[0].text)

book.save("title.xls")
但是,脚本正在覆盖图纸。我只能选择Sheet1或Sheet2,但不能同时选择两者


有什么帮助吗?谢谢。

如果我正确理解了您的需求。很抱歉,无法发表评论以使其更清楚

sheet1 = book.add_sheet('Sheet1', cell_overwrite_ok = True) 
sheet2 = book.add_sheet('Sheet2', cell_overwrite_ok = True)
sheet1.write (cor, 0, site_title[0].text)
sheet2.write (cor, 0, site_title[0].text)

你也可以用熊猫来做

import pandas as pd

# Add your data in list, which may contain a dictionary with the name of the      
# columns as the key
df1 = pd.DataFrame({'website': ['www.dailynews.com', 'www.dailynews.co.zw']})
df2 = pd.DataFrame({'website': ['www.gulf-daily-news.com', 'www.dailynews.gov.bw']})

# Create a new excel workbook
writer = pd.ExcelWriter('title.xlsx', engine='xlsxwriter')

# Write each dataframe to a different worksheet.
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
将numpy导入为np
作为pd进口熊猫
#创建一个数据帧
df1=pd.DataFrame(np.random.rand(100).重塑(50,2),列=['a','b'])
df2=pd.DataFrame(np.random.rand(100).重塑(50,2),列=['a','b'])
#Excel路径
excelpath='path\u to\u your\u excel.xlsx'
#将数据帧写入差异表
使用pd.ExcelWriter(excelpath)作为编写器:
df1.to_excel(书写器,工作表名称='Sheet1')
df2.to_excel(编写器,工作表名称='Sheet2')
“”“我注意到上面的脚本覆盖了中的所有现有列。”
excel。如果您想保持某些列和工作表不变,
你可以这样做:“
作为pd进口熊猫
将numpy作为np导入
从openpyxl导入加载工作簿
book=加载工作簿(excelpath)
writer=pandas.ExcelWriter(excelpath,engine='openpyxl')
writer.book=书
writer.sheets=dict((ws.title,ws)表示book.worksheets中的ws)
df1.to_excel(writer,“Sheet1”,columns=['a','b'])#只填充'a'和'b'列
df2.to_excel(writer,“Sheet2”,columns=['a','b'])#只填充'a'和'b'列
writer.save()

为什么不列出
工作表的
列表?您当然可以有两张工作表,但如果中间没有容器,则无法为两张工作表指定相同的名称。谢谢jonrsharpe。当我运行2个脚本(1个脚本仅用于2个网站)而不是在1个脚本中选择4个网站时,它是否也适用?我不知道你的意思。什么适用?我怀疑两个脚本是否可以访问同一工作簿而不会引起问题。很抱歉造成混淆。我在想:要有2个脚本,每个脚本只选择2个网站。我将一个接一个地运行它们。运行后,我可以在同一个文件中写入两张工作表吗?为什么要这样做?然后你会得到重复的代码。谢谢dexter。如果我想运行2个脚本(每个脚本仅用于2个网站),而不是在一个脚本中选择4个网站,该怎么办?我记得,仅使用xlwt是不可能的,但还有其他方法。请看这里投票最多的答案