Hello,
I am trying to write a python script
that takes record data like this
6xxxxxxxx
7xxxxxxxx
6xxxxxxxx
7xxxxxxxx
7xxxxxxxx
6xxxxxxxx
6xxxxxxxx
6xxxxxxxx
7xxxxxxxx
7xxxxxxxx
7xxxxxxxx
and performs the following logic
newline = ""
read in a record
if the record starts with a 6 and newline = ''
newline = record
if the records starts with a 7
newline = newline + record
if the record starts with a 6 and newline != ''
print newline
newline = record
So it should print out like this:
6xxxxxx 7xxxxxxxx
6xxxxxx 7xxxxxxxx 7xxxxxxx 7xxxxxxx
6xxxxxx
6xxxxxx
etc..
Here is my code:
han1 = open("file","r")
newline = ""
for i in han1:
if i[0] == "6" and newline == "":
newline = i
elif i[0] == "7":
newline = newline + i
elif i[0] == "6" and newline != "":
print newline
newline = ""
newline = i
han1.close()
When I run my script the output looks untouched. Where do you think I'm going wrong. Is it because the newline variable won't store values between iterations of the loop? Any guidance would be appreciated.