1. Technology

How To Create A HTML Calendar In Python Dynamically

By

4 of 10

Splitting the Current Date

To break this bit of data into more managable pieces, we must split it. We can then assign the parts to the variables current_yr, current_month, and current_day respectively.

 current = re.split('-', str(today)) 
 current_no = int(current[1]) 
 current_month = year[current_no-1] 
 current_day = int(re.sub('\A0', '', current[2])) 
 current_yr = int(current[0]) 

To understand the first line of this code, work from the right to the left and from the inside outward. First, we stringify the object today in order to operate on it as a string. Then, we split it using the em-dash as a delimiter, or token. Finally, we assign those three values as a list to 'current'.

In order to deal with these values more distinctly and to call the long name of the current month out of year, we assign the number of the month to current_no. We can then do a bit of subtraction in the subscript of year and assign the month name to current_month.

In the next line, a bit of substitution is needed. The date which is returned from datetime is a two-digit value even for the first nine days of the month. A zero functions as a place holder, but we would rather our calendar have just the single digit. So we substitute no value for every zero that begins a string (hence '\A'). Finally, we assign the year to current_yr, converting it to an integer along the way.

Methods that we will call later will require input in integer format. Therefore, it is important to ensure that all of the date data is saved in integer, not string, form.

Related Video
Create Tables in HTML
Link to Webpages in HTML
  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. Python and HTML - HTML Calendars in Python - Splitting the Current Date

©2014 About.com. All rights reserved.