An algorithm to set text in files to arrays



This post took a little too long to get out. It has been a busy week. Every class I had required me to either write an essay or take a test. I have also started to do some multiplayer HOI4 games with friends.Thus I did not have time to write up this post. At least it is still less than two weeks since the last post. Regardless, here is something I have made that was not a computer science project.

With my work in the prior post about prime factorization, I used some of Python's functions to get a list of prime numbers from a text file. This led me to try some things further with the reading/writing functions.

This is a simple function that takes lines from a text file and puts them into lists. You can find it here on Github. The following is the basic steps:
  1. Check to see what tag is the current line. The line with a tag is indicated by it starting with a hashtag
  2. Add all lines to the list related to that tag until the current line is a tag
  3.  Check to see what tag is the current line again. Check again if it wasn't found the first time.
  4. If no tag was found while checking twice, stop checking.
  5. If the current line is #End, close the file. 
I attempted to abstract the tag checking to the point it could be its own function, but I could not find a workable version. Thus, in order to use it, one will have to prior add the tags into the code by copying and pasting the whole pieces needed. Thankfully the need lines are all together,

As I felt a little wrong being late, I decided to add a little more to the code. I've also added a simple function where I could write in the lines of the file until I put in #End.

The example used for this was the sorting of greetings and goodbyes. I would enter in the tag #Greetings, followed by various greetings one at a time. Next, I would enter in the tag #Goodbyes, followed by various goodbyes one at a time. The algorithm will look for the tags, and put them into their respective list. The algorithm will work even if you switch the order.

This was simply a test of file handling in Python. My next post may possibly be a look back at the idle game I made prior. Until then, see you next time.

Comments

Popular posts from this blog

Version 2:The Not-So Barebones Clicker

A Prime Factorization Algorithm that Uses a Text File

Aesthetico Ultimate Designer - A Python Turtle Program