The datetime function: strptime() DOES check the date for validity. So try
something like:
from datetime import datetime
def get_date():
while True:
try:
date_in = raw_input("Enter date (dd mm ): ")
date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
Using the data you supplied as is, you could try something like:
def print_cities(filename):
# cnt is used just to know if it's time for a newline
cnt = 0
cities = open(filename,'r').readlines()
for city in cities:
# Make sure we have a valid population
if re.match('\d+$',city.spli