Someone came to me today for some help with parsing out a file. I'm just learning python so they said it would be fine if I tried to do this in python.
The basic idea is that they wanted a file that is grouped like this pushed into pipe delimited format
So on and so forth
They wanted the output to look like this
Here's how I did this...I KNOW how I did this was sloppy and wrong, but It was fast and worked. Intellectually it's going to bother me. Here's how I did it:
Specifically how I chose to write the values out was....weird, but like I said it worked...it's just going to drive me nuts.
Python 2.7 by the way.
Thanks everyone!
The basic idea is that they wanted a file that is grouped like this pushed into pipe delimited format
Code:
STRING OF TEXT A
ACCOMPANYING STRING A
ACCOMPANYING STRING A2
STRING OF TEXT B
ACCOMPANYING STRING B
ACCOMPANYING STRING B2
....
So on and so forth
They wanted the output to look like this
Code:
STRING OF TEXT A | ACCOMPANYING STRING A | ACCOMPANYING STRING A2
STRING OF TEXT B | ACCOMPANYING STRING B | ACCOMPANYING STRING B2
...
Here's how I did this...I KNOW how I did this was sloppy and wrong, but It was fast and worked. Intellectually it's going to bother me. Here's how I did it:
Code:
#This is going to need to be parameterized
filename=open("C:\\temp\\source_file.txt", "r")
#Create list (each line is an element)
line = filename.readlines()
#This gives us the grouping of lines (?) This works but has to be wrong/inneficient.
list_length = len(line) / 3
#Create the initial values for the counters
counter = 0
zero = 0
one = 1
two = 2
#Create and empty list to build the full strings into
outputlist = []
#Build the outputlist - I'm basically adjusted the index values that I'm writing into the new list
while counter < list_length:
outputlist_string = line[zero].rstrip()+line[one].rstrip()+"|"+line[two].rstrip()+"\n"
outputlist.insert(counter, outputlist_string)
#Increment the counters
counter += 1
zero = two + 1
one = zero + 1
two = one + 1
#Sort the list
outputlist.sort()
#Print the output
print "".join(outputlist)
filename.close()
Specifically how I chose to write the values out was....weird, but like I said it worked...it's just going to drive me nuts.
Python 2.7 by the way.
Thanks everyone!