如何使用python根据周数确定日期?

时间:2019-06-04 19:22:59

标签: python date

我在学校,我们使用了可怕的学校时间表(SWSCUST)。没有可导出的内容,因此我尝试使用一些发现的代码,仅使其与自己的学校时间表兼容。

我试图获取找到的代码(多亏https://github.com/xcellerator/swscust2ics),以使其适合我自己的学校时间表。我成功地更改了代码,以使其刮擦了我自己的学校时间表(这是荷兰语),但我缺少的是我的学校时间表中的日期。我的确是周数+天(周一,周二等),因此我想解析周数并用python填充空格。

#!/usr/bin/python
from ics import Calendar, Event
from pathlib import Path
from collections import namedtuple
import pandas as pd
import sys
import arrow
import time

(...)

# Open the HTML file as a string
def importCalendarHTML(filename):
    with open(filename, 'r') as inputfile:
        data = inputfile.read()
    return data

# Add a new event to the TimeTable
def AddTimeTableEvent(tt, day, start, end, desc, type, room, staff, dates):
    tt.append(TimeTableEntry(day, start, end, desc, type, room, staff, dates))

# Split the raw HTML into tables via each day
def ParseDays():
    raw_html = importCalendarHTML(sys.argv[1])
    Days = []
    for i in ["maandag", "dinsdag", "woensdag", "donderdag", "vrijdag"]:
        try:
            Days.append(pd.read_html(raw_html, header=0, match=i))
        except:
            continue
    #print(pd.read_html(raw_html, header=0, match=i))
    #print(Days)
    return Days

def WeekDays(day):
    today = datetime.now().date()
    start = today - timedelta(days=today.weekday())
    end = start + timedelta(days=day)
    return end

# Pull out each entry from a table to and compile an entry
def BuildTimeTable(Days):
    TimeTable = []
    for i in range(len(Days)):
        for j in range(len(Days[i])):
            for k in range(len(Days[i][j])):
                day = Days[i][j].get("Dag")[k]
                start = Days[i][j].get("Begintijd")[k]
                end = Days[i][j].get("Eindtijd")[k]
                desc = Days[i][j].get("Opmerking")[k]
                type = Days[i][j].get("Activiteit")[k]
                room = Days[i][j].get("Locatie")[k]
                staff = Days[i][j].get("Docent")[k]
                dates = Days[i][j].get("Weken")[k]
                dates = dates.split(";")
                print(dates)
                #dag = Days[i][j].WeekDays(day)[k]
                AddTimeTableEvent(TimeTable, day, start, end, desc, type, room, staff, dates)
    return TimeTable

(...)

如您所见,理想的情况是将一个新的def接收到星期数,然后给出与该天相对应的日期。我尝试过这样的事情:

def WeekDays(day):
    today = datetime.now().date()
    start = today - timedelta(days=today.weekday())
    end = start + timedelta(days=day)
    return end

(星期一,星期二等)。我的Python知识仍然很基础,这就是为什么我需要您的帮助:) 学校的时间表是:


<p><span >21</span></p>
<table  cellspacing='0' cellpadding='2%' border='1'>
<col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  />
<tr >
<td>Weken</td>
<td>Dag</td>
<td>Begintijd</td>
<td>Eindtijd</td>
<td>Activiteit</td>
<td>Klas</td>
<td>Docent </td>
<td>Locatie</td>
<td>Externe Locatie</td>
<td>Opmerking</td>
</tr>
<tr>
<td>21</td>
<td>dinsdag</td>
<td>9:00</td>
<td>10:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>dinsdag</td>
<td>10:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>dinsdag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>woensdag</td>
<td>8:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>woensdag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>donderdag</td>
<td>8:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>21</td>
<td>donderdag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
</table>

<p><span >22</span></p>
<table  cellspacing='0' cellpadding='2%' border='1'>
<col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  /><col  />
<tr >
<td>Weken</td>
<td>Dag</td>
<td>Begintijd</td>
<td>Eindtijd</td>
<td>Activiteit</td>
<td>Klas</td>
<td>Docent </td>
<td>Locatie</td>
<td>Externe Locatie</td>
<td>Opmerking</td>
</tr>
<tr>
<td>22</td>
<td>maandag</td>
<td>8:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>22</td>
<td>maandag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>22</td>
<td>dinsdag</td>
<td>8:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>22</td>
<td>dinsdag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>22</td>
<td>woensdag</td>
<td>8:00</td>
<td>12:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
<tr>
<td>22</td>
<td>woensdag</td>
<td>13:00</td>
<td>17:00</td>
<td>**Description</td>
<td>**CLASS</td>
<td>**Teacher name</td>
<td>**ROOM</td>
<td>&nbsp;</td>
<td>**Description</td>
</tr>
</table>

1 个答案:

答案 0 :(得分:2)

datetime库允许您create a date using a year, week number, and day of the week name

import datetime

year = "2019"
dates = "27"
day = "dinsdag"
year_week_day = f"{year}/{dates}/{day}"
result_date = datetime.datetime.strptime(year_week_day, "%Y/%W/%A")