There are a large number of different date fromats. Fortunately, the international date format is taking over. This format specifies that dates should be formatted as "YYYY-MM-DDTHH:MM:SS". Where the "T" divides the string into date and time and the other letters stand for Year, Month, Day, Hour, Minute, and Second, respectively. This date is great for international data exchange but it is unfamiliar to most folks. To use the date on a map, we areally need a freindlier version.
import csv TheFile=open("C:\\Users\\Jim\\Desktop\\GSP 318\\Lab07/all_hour.csv","r") # open the file for reading (thus the "r") NumLines=0 TheCSVReader=csv.reader(TheFile) # create an object to read the file as a CSV file NumRows=0 for TheRow in TheCSVReader: # Loop on each line in the file if (NumRows>0): # skip the header line TheDateTimeString=TheRow[0] # get the datetime string print(TheDateTimeString) # print the string for debugging NumRows+=1 TheFile.close() print("Read "+format(NumRows)+" rows from the file")
Note that the date time string has a "T" to divide the date and time. Use the "split()" function to break up this string.
TheDateTimeElements=TheDateTimeString.split("T") # split the date and time into separate strings TheDateString=TheDateTimeElements[0] # get the date string TheTimeString=TheDateTimeElements[1] # get the time string
Next, we can use the dash character to split up the date and obtain strings for each of the date elements.
TheDateElements=TheDateString.split("-") # break up the date elements TheYearString=TheDateElements[0] TheMonthString=TheDateElements[1] TheDayString=TheDateElements[2]
Now we can reformat our date to be in whatever format we desire.
Python Documentation: String functions
Python Documentation: Defining Functions
© Copyright 2018 HSU - All rights reserved.