#!/usr/bin/python import csv fileName = '120629_rpts_wind.csv' #newFile = '120629_abbrev_wind.txt' newFile = '120629_full_wind.txt' reportStart = "20120629" reportEnd = "20120630" header1='(index)->(Time,Latitude,Longitude,Speed,Location(Text),County(Text),State(Text),sTime(Text))' header2='[fmt="yyyyMMdd HH:mm:ss"],Latitude[unit="degrees"],Longitude[unit="degrees"],Speed[unit="kts"],Location(Text)[],County(Text)[],State(Text)[],sTime(Text)[]' # open in Universal New Line Mode (rU) to handle empty cells in excel document readReports = csv.reader(open(fileName, 'rU'), dialect='excel') # open a new file csv.register_dialect("EscapeQuotes",quoting=csv.QUOTE_MINIMAL,doublequote=False,escapechar="\\") newFileWrite = csv.writer(open(newFile, 'wb'),'EscapeQuotes') newFileWrite.writerow([header1]) newFileWrite.writerow([header2]) # The each row in the report is a list of the excel file cells. # The report from SPC is from 12Z on previous day to 12Z on current day # Need to dicipher time information and add the date # Use count to skip the text header count = 0 for row in readReports: if (count > 0): ### split the time if it is already formatted, so the time consturction block works for both formatted and unformatted cases splitTime = row[0].split(":") strTime="" if (len(splitTime) > 0): for x in range(len(splitTime)): strTime = strTime + splitTime[x] else: strTime = splitTime ### all time will be 4 digits, zero pad right ==> zfill(4) strTime = strTime.zfill(4) ### if time is greater than 12Z, then it is previous day formatTime = strTime[0:2].zfill(2) + ":" + strTime[2:4].zfill(2) + ":" + "00" if (int(strTime) > 1200): dayTime = reportStart + " " + formatTime ### else report is current day else: dayTime = reportEnd + " " + formatTime speed = row[1] location = '"'+row[2]+'"' county = row[3] state = row[4] lat = float(row[5]) lon = float(row[6]) outTime = '"'+strTime + " UTC"+'"' if ((speed != 'UNK') and (int(speed) > 57)): newFileWrite.writerow([dayTime, lat, lon, speed, location, county, state, outTime]) count+=1 #readReports.close() #newFileWrite.close()