Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14301

Problem counting activities and updating column in Python

$
0
0

So I have a input where I have like 16 columns, I'm trying to count the rows that I get in my output, so I can find the last row that contains the "BIDGROUP" element inside and place the count in its seventh column, an example:

INPUT:

"131594", "", "BIDGROUP", 1, 0, 0, 2, "", 0:00, 0:00, 01JAN2009, 01JAN2009, 01JAN2009, 01JAN2009, false, 0,"131594", "AWARD", "UNTOUCHABLE", 1, 1, 0, 1, "", 0:00, 0:00, 10JUN2014, 13JUN2014 23:59, 01JAN2009, 01JAN2009, false, 100,"131594", "AWARD", "ADVANCED_TRIP", 1, 2, 0, 0, "740025Jun2014,705406Jun2014,737722Jun2014,696130Jun2014", 0:00, 0:00, 01JAN2009, 01JAN2009, 01JAN2009, 01JAN2009, false, 15,

EXPECTED OUTPUT:

"131594", "", "BIDGROUP", 1, 0, 0, 5, "", 0:00, 0:00, 01JAN2009, 01JAN2009, 01JAN2009, 01JAN2009, false, 0,"131594", "AWARD", "UNTOUCHABLE", 1, 1, 0, 1, "", 0:00, 0:00, 10JUN2014, 13JUN2014 23:59, 01JAN2009, 01JAN2009, false, 100,"131594", "AWARD", "TRIP_ID", 1, 2, 0, 0, "7400", 0:00, 0:00, 25Jun2014, 01JAN2009, 01JAN2009, 01JAN2009, false, 15,"131594", "AWARD", "TRIP_ID", 1, 3, 0, 0, "7054", 0:00, 0:00, 06Jun2014, 01JAN2009, 01JAN2009, 01JAN2009, false, 15,"131594", "AWARD", "TRIP_ID", 1, 4, 0, 0, "7377", 0:00, 0:00, 22Jun2014, 01JAN2009, 01JAN2009, 01JAN2009, false, 15,"131594", "AWARD", "TRIP_ID", 1, 5, 0, 0, "6961", 0:00, 0:00, 30Jun2014, 01JAN2009, 01JAN2009, 01JAN2009, false, 15,

If you see in the input the first row has the element "BIDGROUP", and the seventh column has a count of 2, because there are 3 activities only, and in the output the counter change to 5 because we have 6 activities in total.My output is almost the same, as I said before I only have to count the rows, I already tried to set a count for all the activities but it didn't work like I expected.This is my original code that was used without the counter:

import sysimport relines = []for line in sys.stdin:    lines.append(line.strip())output_lines = []for line in lines:    elements = line.split(", ")    if elements[2] == '"ADVANCED_TRIP"':        elements[2] = '"TRIP_ID"'        trip_ids = elements[7].split(",")        dates = re.findall(r'\d{2}[A-Za-z]{3}\d{4}', line)        for i, (trip_id, date) in enumerate(zip(trip_ids, dates)):            trip_id = trip_id.strip('"')            output_line = elements[:7] + [f'"{trip_id[:4]}"'] + elements[8:]            output_line[4] = str(int(output_line[4]) + i)            output_line[10] = date            output_lines.append(output_line)    else:        output_lines.append(elements)for output_line in output_lines:    print(", ".join(output_line))

And this is the try that I did whit the count of all the activities:

import sysimport relines = []for line in sys.stdin:    lines.append(line.strip())output_lines = []total_activity_count = 0trip_id_activity_count = 0for line in lines:    elements = line.split(", ")    if elements[2] == '"BIDGROUP"':        total_activity_count += 1        elements[6] = str(total_activity_count)        output_lines.append(elements)    elif elements[2] == '"ADVANCED_TRIP"':        elements[2] = '"TRIP_ID"'        trip_ids = elements[7].split(",")        dates = re.findall(r'\d{2}[A-Za-z]{3}\d{4}', line)        for i, (trip_id, date) in enumerate(zip(trip_ids, dates)):            trip_id = trip_id.strip('"')            output_line = elements[:7] + [f'"{trip_id[:4]}"'] + elements[8:]            output_line[4] = str(int(output_line[4]) + i)            output_line[10] = date            output_lines.append(output_line)            trip_id_activity_count += 1    else:        output_lines.append(elements)        total_activity_count += 1for output_line in output_lines:    print(", ".join(output_line))

Anyone have abother idea how could this work?


Viewing all articles
Browse latest Browse all 14301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>