使用 xlsxwriter 将文件命名为当前日期和时间
Use xlswriter to name a file as current date and time
我正在尝试使用 xlsxwriter 创建一个 excel 文件并将文件命名为当前日期和时间。对于上下文,我想将其添加到网络抓取工具中,每天中午设置为 运行 并将其导出到 Excel。我希望文件名与抓取时间相对应。
我试过使用 datetime 函数但没有成功:
import xlsxwriter
import datetime
# Create a workbook and add a worksheet.
todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
有谁知道为什么这不起作用或其他选择?
@Sandeep Hukku - 编辑代码如下:
import xlsxwriter
import datetime
# Create a workbook and add a worksheet.
# todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )+ '.xlsx'
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
@Snehal Parmar - 第二次更新:
导入 urllib
导入 urllib.request
从 bs4 导入 BeautifulSoup
导入日期时间
导入 xlsxwriter
# Web scraping
def make_soup(url):
the_page = urllib.request.urlopen(url)
soup_data = BeautifulSoup(the_page, "html.parser")
return soup_data
soup = make_soup('http://www.url.co.uk')
def getNames():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('td', {"class": "propname"}):
print(td_in_data.text)
def getRooms():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('span', {"class": "beds"}):
print(td_in_data.text)
def getRents():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('td', {"class": "rentprice"}):
print(td_in_data.text)
''' To do: get the scraped data to an Excel doc.'''
# Create a workbook and add a worksheet.
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )+ '.xlsx'
todays_date = todays_date.replace(" ", "_").replace(":", "_")
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Data to Excel.
Excel_dump = (
['Name', getNames()],
['Rent', getRents()],
['Rooms', getRooms()]
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in Excel_dump:
worksheet.write()
worksheet.write(col, row, item)
worksheet.write(col, row + 1)
row += 1
你只需要写
todays_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx'
而不是
todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
我不确定,但如果您在命名后关闭文件,然后再次以追加模式打开,然后将数据添加到文件中,则可能会完成这项工作。有时创建文件是惰性操作,所以这里发生的是直到没有任何内容写入文件,它不会创建文件,操作系统也起着重要作用。请让我知道,因为它的网络抓取程序代码我无法在我的机器上模拟。
遇到问题:
问题出在文件名中的时间部分,
添加以下内容:
todays_date = todays_date.replace(" ", "_").replace(":", "_")
或
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d_%H_%M") )+ '.xlsx'
我希望这能解决问题。
我正在尝试使用 xlsxwriter 创建一个 excel 文件并将文件命名为当前日期和时间。对于上下文,我想将其添加到网络抓取工具中,每天中午设置为 运行 并将其导出到 Excel。我希望文件名与抓取时间相对应。
我试过使用 datetime 函数但没有成功:
import xlsxwriter
import datetime
# Create a workbook and add a worksheet.
todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
有谁知道为什么这不起作用或其他选择?
@Sandeep Hukku - 编辑代码如下:
import xlsxwriter
import datetime
# Create a workbook and add a worksheet.
# todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )+ '.xlsx'
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
@Snehal Parmar - 第二次更新:
导入 urllib 导入 urllib.request 从 bs4 导入 BeautifulSoup 导入日期时间 导入 xlsxwriter
# Web scraping
def make_soup(url):
the_page = urllib.request.urlopen(url)
soup_data = BeautifulSoup(the_page, "html.parser")
return soup_data
soup = make_soup('http://www.url.co.uk')
def getNames():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('td', {"class": "propname"}):
print(td_in_data.text)
def getRooms():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('span', {"class": "beds"}):
print(td_in_data.text)
def getRents():
for record in soup.findAll('tr'):
for data in record.findAll('td'):
for td_in_data in data.findAll('td', {"class": "rentprice"}):
print(td_in_data.text)
''' To do: get the scraped data to an Excel doc.'''
# Create a workbook and add a worksheet.
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )+ '.xlsx'
todays_date = todays_date.replace(" ", "_").replace(":", "_")
workbook = xlsxwriter.Workbook(todays_date)
worksheet = workbook.add_worksheet()
# Data to Excel.
Excel_dump = (
['Name', getNames()],
['Rent', getRents()],
['Rooms', getRooms()]
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for item, cost in Excel_dump:
worksheet.write()
worksheet.write(col, row, item)
worksheet.write(col, row + 1)
row += 1
你只需要写
todays_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx'
而不是
todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"
我不确定,但如果您在命名后关闭文件,然后再次以追加模式打开,然后将数据添加到文件中,则可能会完成这项工作。有时创建文件是惰性操作,所以这里发生的是直到没有任何内容写入文件,它不会创建文件,操作系统也起着重要作用。请让我知道,因为它的网络抓取程序代码我无法在我的机器上模拟。
遇到问题: 问题出在文件名中的时间部分, 添加以下内容:
todays_date = todays_date.replace(" ", "_").replace(":", "_")
或
todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d_%H_%M") )+ '.xlsx'
我希望这能解决问题。