python_code_snippets
This is an old revision of the document!
Python code snippets
Handling arguments
import argparse
parser = argparse.ArgumentParser(description='Collect arguments')
parser.add_argument("-t", required=True, type=str, help="Train number")
parser.add_argument("-s", required=True, type=str, help="Station")
parser.add_argument("-d", required=True, type=str, help="Date")
args = parser.parse_args()
train_num = args.t
station = args.s
date = args.d
print(train_num)
print(station)
print(date)
Date and time strings
from datetime import datetime
date_time_str = '2020-09-07T00:27:00-04:00'
date_time_str = date_time_str[:-6]
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S')
#print("The type of the date is now", type(date_time_obj))
#print("The date is", date_time_obj)
date_time_str_format = date_time_obj.strftime('%I:%M %P')
print(date_time_str_format)
Checking and matching stuff in strings
[[https://thispointer.com/python-check-if-a-string-contains-a-sub-string-find-its-index-case-insensitive/|String Matches]
status = "Service disruption"
if "Disruption".lower() in status.lower():
print('SD so exiting')
python_code_snippets.1599605745.txt.gz · Last modified: by juckins
