''' DVC SCRIPT Brett Paufler Copyright (c) 2021-04-23/24 A Python Script, which transforms dvc_transcript.txt into HTML Formatted Sections for use in a BrettRant This is a one off script. ''' #I have my preferred workflow FILE_IN = "./input/dvc_transcript.txt" FILE_OUT = "./output/dvc_formatted.txt" #The input is read and saved to a working variable with open(FILE_IN, 'r') as f: raw_text = f.read() #print raw_text #Monolithic Text Split into Lines lines = raw_text.split('\n') #print lines #Endless Prints #print len(lines) #I check as I go #Trims Off The Front Matter, leaving CSV Data #I almost always get the number by Plugging & Chugging lines = lines[24:] #print lines #Leading & Ending Values are Correct #print len(lines) #64 is the Correct Number of Courses #Normally, this would go at the top #But this is where I added it, so here it goes from collections import namedtuple #These are the Final Fields I want #They differ from the CSV Fields Course = namedtuple('Course', ['catalog', 'section', 'name', 'grade', 'credit', 'term']) #print Course #Test Case without a Section Number test_course_ns = Course( 'ECON-220', '', 'Principles of MacRoeconomics', 'A','3','1992SP') #print test_course_ns #Test Case with a Section Number test_course = Course( 'HSCI-126', '8931', 'Stress Management and Health', 'A','3','2006SP') #print test_course #I'm going to Loop through the Lines #And transform each line into a Course #This is where I will store the Courses course_list = [] #The Main Data Munging Loop for line in lines: #What could be simpler? #print line #let's see what we are working with csv_values = line.split(',') #print csv_values #The End Values are Easier to Deal With #So, I'll work from the back-end, popping values #It seemed like a silly check at the time #But I have an Error in "Magic, Witchcraft, & Religion" #assert len(csv_values) == 4 #This Handles My One Length Exception if len(csv_values) != 4: #print csv_values start_string = ','.join(csv_values[:3]) #Combine Three as One start_string = start_string.replace('"', '') #Kill the added "'s csv_values = [start_string] + csv_values[3:] #print csv_values assert len(csv_values) == 4 #Now, where was I? #print csv_values #It All Looks Good #There are easier ways to do this #But they require more thought term = csv_values.pop() credit = csv_values.pop() grade = csv_values.pop() #print grade, credit, term #All that remains in csv_values is the start string assert len(csv_values) == 1 #Yep, I was right #I need to pull catalog, section, & name cat_string = csv_values.pop() assert len(csv_values) == 0 #Can't Hurt string_pieces = cat_string.split() catalog = string_pieces.pop(0) #easiest first #If at first you don't succeed #The exception code will run try: section = str(int(string_pieces[0])) #If Error (i.e. no Int Conversion) string_pieces = string_pieces[1:] #Then this never fires except: section = '' #Now, we just rejoin the pieces name = ' '.join(string_pieces) #print catalog, section, name #It All Looks Good, So Let's Build a Named_Tupel this_course = Course( catalog, section, name, grade, credit, term) #print this_course #Looks Good (Well, it does now) course_list.append(this_course) #Credits is a Keyword, listing Python Credits #I'd wanted to use that in my Tuple #But nope, not going to happen #print len(course_list) #64, which is correct # # # #The Munging Done (Data to Types) #I will break here #Output is next #But it's time for a break # # # #Starting This Section #All Other Prints Commented Out #print len(course_list) #print course_list[0] #All Types are Strings #Let's see if this works test_first = "Carry S Through: %s %%s" % "First" test_second = test_first % "Second" #print test_first #print test_second #So, that Works #Normally, I'd do that in a "Workspace" #Or Delete it for clarity #But this file is about My Workflow #Let's Build Up a Wall of Text text = "" for c in course_list: text += '\n\n
\n
\n
\n' text += c.catalog if c.section: text += " (%s)" % c.section text += "
\n" #This is Confusing, But it's done #And for a Script, that is good enough text += c.term if len(c.credit) == 1: text += ": %s.0-%s" % (c.credit, c.grade) else: text += ": %s-%s" % (c.credit, c.grade) text += "
\n" text += c.name text += "
\n
\n
\n" text += '
\n
\n
\n' #These Prints Will Carry Down #They Started at the top of the Loop #And I built the Loop on Top of them #I run the Program at every additional change print text print course_list[0] #We have the desired text #Output as Displayed with open("./output/transcript_text.txt", "w") as f: f.write(text) #But I also want to check my Output Right Now html = "%s" % text test_text = 50 * "Test Text! " #print test_text #Yep, it works #Once again, normally IO would delete div_now = '
\n' div_then = '
\n%s' % test_text html = html.replace(div_now, div_then) #print html #And now, the html has placeholding gibberish text #Is This Formatted Correctly? Let's see with open("./output/transcript_html.html", "w") as f: f.write(html) """ And that's that. What isn't shown is where and when I commented prints And the extent to which the final text was reordered Once I outputted the HTML, I used that as my goby And rejiggered the output HSCI-126 (8931): 3.0-A (2006SP) Stress Management and Health or something like the above, became HSCI-126 (8931) 2006SP: 3.0-A Stress Management and Health I will apply css styling to achieve the desired result, likely font-weight: bold; The timing is roughly an hour a day for two days I will copy/paste transcript_text into Rant_Blank And start to fill in the course_comments I doubt I saved any time from doing the formatting manually Two Hours is a lot of time But the time was way more enjoyably spent And for the most, All time spent programming Leads to better programming Also, This Script has become Content Yippie! """ """ And here starts the endless comment strings or none at all. Originally, I'd thought of solving this problem by rearranging Cells in Excell (or more accurately, OpenCalc) And then, using NPP (N++, and/or NotePad++, they all being the same to rearrange rows. If I knew what I wanted from the start, that route would have been faster But at the end, when I made a change, I lost little work. I had my Python Object and formmating output went quick. I guess I'm trying to justify those two hours. It seems like a fair clip of time considering the output. """