Reputation: 87
I'm searching for a 'pythonic' solution to a fairly straightforward problem, yet one I can't seem to wrap my head around.
I have an input file, like the one I've sampled below:
Time,F_Scale,Location,County,State,Lat,Lon,Comments
Time,Speed,Location,County,State,Lat,Lon,Comments
1530,UNK,ASHLAND,AROOSTOOK,ME,46.63,-68.4,LARGE TREE LIMBS DOWN AT ASHLAND COMMUNITY LIBRARY. REPORTED VIA SOCIAL MEDIA. (CAR)
1555,UNK,WASHBURN,AROOSTOOK,ME,46.79,-68.16,LARGE TREE DOWN IN WASHBURN VIA SOCIAL MEDIA. TIME ESTIMATED BASED ON RADAR. (CAR)
1600,70,4 ENE WASHBURN,AROOSTOOK,ME,46.8,-68.07,SMALL BIRCH TREE DOWN ALONG CARIBOU LAKE RD. (CAR)
1610,UNK,3 ENE WOODLAND,AROOSTOOK,ME,46.9,-68.08,TREE DOWN AND BLOCKING RT. 161. TIME ESTIMATED BASED ON RADAR. (CAR)
Time,Size,Location,County,State,Lat,Lon,Comments
1726,100,WESTFIELD,HAMPDEN,MA,42.14,-72.76,AMATEUR RADIO CONFIRMED BY BROADCAST MEDIA (BOX)
I've ingested the file, and split it by commas--but I'm trying to find a way to have python print out every line, starting with when it finds 'Speed' as the first index and stopping when it finds 'Size' as the first index. So, the result would be:
Time,Speed,Location,County,State,Lat,Lon,Comments
1530,UNK,ASHLAND,AROOSTOOK,ME,46.63,-68.4,LARGE TREE LIMBS DOWN AT ASHLAND COMMUNITY LIBRARY. REPORTED VIA SOCIAL MEDIA. (CAR)
1555,UNK,WASHBURN,AROOSTOOK,ME,46.79,-68.16,LARGE TREE DOWN IN WASHBURN VIA SOCIAL MEDIA. TIME ESTIMATED BASED ON RADAR. (CAR)
1600,70,4 ENE WASHBURN,AROOSTOOK,ME,46.8,-68.07,SMALL BIRCH TREE DOWN ALONG CARIBOU LAKE RD. (CAR)
1610,UNK,3 ENE WOODLAND,AROOSTOOK,ME,46.9,-68.08,TREE DOWN AND BLOCKING RT. 161. TIME ESTIMATED BASED ON RADAR. (CAR)
I've tried a variety of terrible loops with attempted methods of trying to flag when the first index is 'Speed', but nothing has worked. I won't bore you with my unsuccessful attempts in lieu of embarrassing myself.
As a python newbie, I'm sure there has to be a proper pythonic way to do this...even by possibly crafting a 'for loop' that plays off the value of the first index? But my amateur python status is holding me back, apparently.
Upvotes: 0
Views: 77
Reputation: 5232
You'll have to create a boolean flag to tell you when to print. However, you can just break out of the loop when you reach 'Size'.
with open(myfile) as f:
reached_speed = False
for line in f:
second_item = line.split(',')[1]
# stop printing if Size
if reached_speed and second_item == 'Size':
break
# start printing if Speed
elif not reached_speed and second_item == 'Speed':
reached_speed = True
if reached_speed:
print(line)
Upvotes: 2
Reputation: 26
My Python version is python3.7.
def loop_file(file):
print_line = False
with open(file, encoding='utf-8') as f:
for line in f:
split = line.split(',')
if len(split) > 1:
item_index_one = split[1]
if item_index_one == 'Speed':
print_line = True
elif item_index_one == 'Size':
break
if print_line:
print(line)
if __name__ == '__main__':
loop_file('your file path')
Upvotes: 0
Reputation: 495
You wrote that you already parsed the file and split at the commas, so I assume you have the contents in a variable called data which is a list of lists.
while data[0][1] != "Speed":
data.pop(0)
while data[0][1] != "Size":
print (",".join(data.pop(0)))
Note that the above code has no error checking. You might want to make sure that there actually is a sequence of Speed/Size lines, and that each line has at least two fields.
Upvotes: 0