Python Advanced #10 - Reading Two Columns from a Non-Justified Text File and Saving into Lists

In this post, we start with a non-justified text file as shown below.  This file is titled nonjust.txt.




Our objective is to extract the first and last columns and then save those into lists.  I will be exporting them to a different text file in a future post.  We do this by using the code below.

We first open the text file that contains the text.  We create empty lists to store the first column (vals1) and to store the last column (vals2).  An additional empty list (templist) is used to store the values found on each line.  In the while loop, we first use the split command on the current line to create three separate value to populate templist.  Without using the split command, we would have a single string.  We then append the first value (position 0 in templist) to vals1 and append the third value (position 2 in templist) to vals2.  We then clear templist of its contents and read the next line in the text file.  After the while loop concludes, we then print vals1 and vals2 to check the contents of the list.



The results from this program are shown below.




Comments