Thursday, July 17, 2014

How to add time

Code
from datetime import datetime, timedelta                   #1
datetime.now()                                             #2
datetime.now() + timedelta(hours=10)                       #3
datetime.now() + timedelta(minutes=10)                     #4
datetime.now() + timedelta(seconds=10)                     #5


How this works - Line by Line

from datetime import datetime, timedelta             #1

Imports the datetime and timedelta classes from the datetime module

datetime.now()                                       #2
 
This returns a datetime object with the current time.

datetime.now() + timedelta(hours=10)                 #3
 
This returns a datetime object with the current time + 10 hours. 

The remaining lines should be self explanatory.  Interestingly, the timedelta class also allows you to add by days and weeks, but not by months or years.


How to get the alphabet range

Code
import string                                              #1
string.ascii_lowercase                                     #2

How this works - Line by Line

import string                                        #1

Imports the string module

string.ascii_lowercase                               #2
 
This returns the alphabet range in lower case.

How to determine if a folder exists

Code
import os                                              #1
os.path.exists(folder_path_string)                     #2

How this works - Line by Line

import os                                           #1

Imports the os module

os.path.exists(folder_path_string)                  #2
 
This line checks for a folder in the folder_path_string and returns either True or False

Wednesday, July 16, 2014

How to delete a file

Code
import os                                              #1
os.remove(file_path_string)                            #2

How this works - Line by Line

import os                                           #1

Imports the os module

os.remove(file_path_string)                         #2
 
This line deletes the file located in the file_path_string.  (An example file_path_string would be 'c:\temp\test.xls')

How to count the number of items in a list

Code
mylist = ['apples','bananas', 'oranges']                               #1
len(mylist)                                                            #2

How this works - Line by Line

mylist = ['apples','bananas', 'oranges']                               #1
 
Defining mylist as a example list of items


len(mylist)                                                            #2

This line returns the count of items in the list.  In this case, the result would be 3.

How to replace text within a string

Code
oldstring = 'This is the string with text that needs to be replaced'   #1
newstring = oldstring.replace('needs to be ','had text ')              #2
print newstring                                                        #3


How this works - Line by Line

oldstring = 'This is the string with text that needs to be replaced'   #1
 
Defining oldstring to be string of text which i will use as an example


newstring = oldstring.replace('needs to be ','had text ')              #2

This line replaces text within the old string. 

print newstring                                                        #3

Printing the newstring to show that the desired text has been replaced.

How to convert a unicode date to mm/dd/yyyy

Code
from datetime import datetime                                          #1
unicodeDate = 'Jul162014'                                              #2
print datetime.strptime(unicodeDate,'%b%d%Y').strftime('%m/%d/%Y')     #3


How this works - Line by Line

from datetime import datetime                                          #1
 
Imports the datetime class from the datetime module


unicodeDate = 'Jul162014'                                              #2

I'm creating an example variable for the date in unicode format and calling it unicodeDate

print datetime.strptime(unicodeDate,'%b%d%Y').strftime('%m/%d/%Y')     #3

I'm printing the results of calling two methods.  The first method, strptime is taking the unicodeDate and the format of the date, '%b%d%Y', and returns a datetime object.  This datetime object is then converted into a string by the strftime method and output in the requested format '%m/%d/%Y'.

How to convert a datetime stamp in YYYYMMDD HH:MM:SS format to a datetime object

Code
from datetime import datetime                                              #1
datetimestring = '20140714 04:05:10'                                       #2
datetimeobject = datetime.strptime(datetimestring,'%Y%m%d %H:%M:%S')       #3
print datetimeobject                                                       #4
 


How this works - Line by Line

from datetime import datetime                                              #1
 
Imports the datetime class from the datetime module, which is standard


datetimestring = '20140714 04:05:10'                                       #2

Creating an example date time string in the YYYYMMDD HH:MM:SS format.

datetimeobject = datetime.strptime(datetimestring,'%Y%m%d %H:%M:%S')       #3

Converts the datetimestring into a datetimeobject using the datetime class's strptime method.


print datetimeobject                                                       #4
 
Prints the datetimeobject. This datetimeobject can then be reconverted into another format using the strftime method. i.e. datetimeobject.strftime('%m%d%Y %H%M%S')

iPython Notebook Keyboard Shortcuts

Shortcut Action
Shift-Enter run cell
Ctrl-Enter run cell in-place
Alt-Enter run cell, insert below
Ctrl-m x cut cell
Ctrl-m c copy cell
Ctrl-m v paste cell
Ctrl-m d delete cell
Ctrl-m z undo last cell deletion
Ctrl-m - split cell
Ctrl-m a insert cell above
Ctrl-m b insert cell below
Ctrl-m o toggle output
Ctrl-m O toggle output scroll
Ctrl-m l toggle line numbers
Ctrl-m s save notebook
Ctrl-m j move cell down
Ctrl-m k move cell up
Ctrl-m y code cell
Ctrl-m m markdown cell
Ctrl-m t raw cell
Ctrl-m 1-6 heading 1-6 cell
Ctrl-m p select previous
Ctrl-m n select next
Ctrl-m i interrupt kernel
Ctrl-m . restart kernel
Ctrl-m h show keyboard shortcuts

How to get the current time in Python

Code
from time import strftime                                              #1
print strftime("%H:%M:%S")                                             #2


How this works - Line by Line

from time import strftime                                              #1
 
Imports the strftime function from the time module, which is standard


print strftime("%H:%M:%S")                                             #2

Prints the current local time.

How to convert date formats from YYYYMMDD to MM/DD/YYYY

YYYYMMDD2MM-DD-YYYY
In [1]:
from datetime import datetime
In [2]:
oldformat = '20140716'
datetimeobject = datetime.strptime(oldformat,'%Y%m%d')
In [3]:
newformat = datetimeobject.strftime('%m-%d-%Y')
print newformat
07-16-2014

In [4]:
newformat2 = datetimeobject.strftime('%m/%d/%Y')
print newformat2
07/16/2014

In [5]:
#Here is a link to the python strftime character references
#http://strftime.org/