-
Notifications
You must be signed in to change notification settings - Fork 1
/
txtToCsv.py
133 lines (120 loc) · 4.11 KB
/
txtToCsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
## @file txtToCsv.py
#
# @brief this file converts TXT file to CSV
# Copyright 2022, Jodie Moh, All rights reserved.
#
# @author Jodie Moh
import os, sys
import fileIo
import datetime
from os.path import expanduser, isdir
import easygui
import settings
def parseScheduleTxt():
classCode = settings.classCode
locationCode = settings.locationCode
classTypes = settings.classTypes
classNum = settings.classNum
start_dir = '~/'
if isdir(expanduser("~/Desktop")):
start_dir = '~/Desktop/'
msg = 'Please select the .txt file to be converted to .csv'
scheduleTxt = easygui.fileopenbox(msg=msg, title="", default=expanduser(start_dir), filetypes=["*.txt"])
if(scheduleTxt[-3:] != "txt"):
raise Exception
timeCode = ["AM -", "PM -"]
newClass = "Class Nbr"
currentDateTime = datetime.datetime.now()
date = currentDateTime.date()
currentYear = date.strftime("%Y")
#read file
listOfClass = fileIo.fileToList(scheduleTxt)
csvList = ["Subject,Start Date,Start Time,End Date,End Time,Location,Description"]
# Subject - class name + type
# Start Date - MM/DD/YYYY
# Start Time - 24 hour format (13:45) or 12 AM/PM (01:45 PM)
# End Date
# End Time
# Location
# Description
#As of Python version 3.7, dictionaries are ordered.
#In Python 3.6 and earlier, dictionaries are unordered.
csvString = {
"subject" : "NA",
"startDate" : "NA",
"startTime" : "NA",
"endDate" : "NA",
"endTime" : "NA",
"location" : "NA",
"description" : "NA"
}
def appendToCSVList():
temp = ""
for x in csvString.values():
temp += x
temp += ","
csvList.append(temp[:-1]) #remove last comma
def clearDict(clearAll):
for x in csvString.keys():
if(not clearAll and x == "subject"):
continue
csvString[x] = "NA"
def changeDateFormat(date):
#change from DD/MM/YYY to MM/DD/YYYY
date = date.split("/")
newDate = date[1] + "/" + date[0] + "/" + date[2]
return newDate
profName = False
module=""
for x in listOfClass:
if(x.strip() == ""):
continue
for y in classCode:
if y in x:
module = x
continue
for y in classTypes:
if y in x:
csvString["subject"] = csvString["subject"][:-1]
csvString["subject"] += " " + y + ")"
continue
for y in timeCode:
if y in x:
timeSplit = x.split(" ")
csvString["startTime"] = timeSplit[1]
csvString["endTime"] = timeSplit[3]
continue
for y in classNum:
if y in x:
csvString["subject"] = module + " ("
csvString["subject"] += x + ")"
continue
if currentYear in x:
dateSplit = x.split(" ")
csvString["startDate"] = changeDateFormat(dateSplit[0])
csvString["endDate"] = changeDateFormat(dateSplit[2])
profName = False
appendToCSVList()
#clear dictionary
clearDict(False)
if(profName):
if("Professor" in csvString["description"]):
csvString["description"] += " "
csvString["description"] += x.replace(",","")
else:
csvString["description"] = "Professor(s): "
csvString["description"] += x.replace(",","")
continue
for y in locationCode:
if y in x:
csvString["location"] = x
profName = True #after location is the professors name
break
return csvList
def createCSVFile(data):
# Write final .ics file to same directory as input file.
fileName = easygui.filesavebox(msg='Save .csv File', title='', default=expanduser('~/') + 'calendar.csv', filetypes=['*.csv'])
if(fileName is not None):
with open(fileName, "w", encoding="utf-8") as f:
for l in data:
f.write(l+"\n")