I did this "wrong" in python I think. Suggestions?

Kaos

[H]ard|Gawd
Joined
Oct 14, 2003
Messages
1,328
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

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!
 
Loop over the input list. Division will give you the output line number; the mod operator (%) will tell you whether to add a pipe or a newline.
 
Loop over the input list. Division will give you the output line number; the mod operator (%) will tell you whether to add a pipe or a newline.

I considered doing mod 3 == 0 etc but for some reason I got all hung up on the sort. I'd still need some sort of intermediary list to be able to sort them properly, right?

Thanks!
 
I considered doing mod 3 == 0 etc but for some reason I got all hung up on the sort. I'd still need some sort of intermediary list to be able to sort them properly, right?
Yes, but you can still build the same list you're building now (just with about ten fewer lines of code). Just loop over the input lines, and put each one in the right list position with the right separator.

Or if you want to stick with your approach, grabbing three lines at a time, at least get rid of all those counters... You only need one.
 
Back
Top