简体   繁体   English

使用xlswriter将文件命名为当前日期和时间

[英]Use xlswriter to name a file as current date and time

I am trying to use xlsxwriter to create an excel file and name a file as the current date and time. 我正在尝试使用xlsxwriter创建一个excel文件并将文件命名为当前日期和时间。 For context, I want to add this into a web scraper, which is set to run at midday everyday and export it to Excel. 对于上下文,我想将其添加到Web scraper中,该Web scraper设置为每天中午运行并将其导出到Excel。 I want the file names to correspond with the scrape time. 我希望文件名与刮擦时间一致。

I have tried using the datetime function with no success: 我尝试过使用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()

Does anyone know why this is not working or another alternative? 有谁知道为什么这不起作用或另一种选择?

@Sandeep Hukku - edited code below: @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 - second update: @Snehal Parmar - 第二次更新:

import urllib import urllib.request from bs4 import BeautifulSoup import datetime import xlsxwriter 从bs4导入urllib import urllib.request导入BeautifulSoup导入datetime导入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

I'm not sure but if you close the file after naming it and then again open in append mode and then add data into the file that might do the job. 我不确定,但如果你在命名后关闭文件,然后再以附加模式打开,然后将数据添加到可能完成工作的文件中。 Sometimes create file is lazy operation, so what happens here is until nothing is written into the file its not create file and also operating system play important role too. 有时创建文件是懒惰的操作,所以这里发生的事情是,直到没有任何内容写入文件,它不是创建文件,操作系统也起着重要的作用。 Please and let me know, as its web scraper code I can't simulate here on my machine. 请让我知道,因为我的网络刮刀代码我无法在我的机器上模拟。

Got the issue: the problem is with the time part in the name of file, add the following : 得到了问题:问题在于文件名中的时间部分,添加以下内容:

todays_date = todays_date.replace(" ", "_").replace(":", "_")

or 要么

todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d_%H_%M") )+ '.xlsx'

I hope this will fix the issue. 我希望这能解决这个问题。

你只需要编写todays_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx'而不是todays_date = "'" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + '.xlsx' + "'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM