sorting - Python – sum up/sort range of numbers from file -
sorting - Python – sum up/sort range of numbers from file -
this first post here, sorry if i've done wrong, , i'll seek explain best can. have 2 files, 1 csv/txt file named text1.txt in next format:
class="lang-none prettyprint-override">"13:02",10 "13:03",30 "13:04",15 "13:05",12 "13:06",3
…and (plain text) file named console1.txt this:
class="lang-none prettyprint-override">rate limit: 5 @ thu jun 12 13:02:00 pdt 2014 (total missed: 5) rate limit: 10 @ thu jun 12 13:02:01 pdt 2014 (total missed: 15) rate limit: 17 @ thu jun 12 13:02:06 pdt 2014 (total missed: 32) rate limit: 10 @ thu jun 12 13:05:50 pdt 2014 (total missed: 42) rate limit: 14 @ thu jun 12 13:05:53 pdt 2014 (total missed: 56) rate limit: 84 @ thu jun 12 13:05:21 pdt 2014 (total missed: 140) rate limit: 2 @ thu jun 12 13:06:30 pdt 2014 (total missed: 142) rate limit: 5 @ thu jun 12 13:06:34 pdt 2014 (total missed: 147)
i want sum these numbers total number "rate limited" each minute, , add together these corresponding rows in first csv/txt file. so, intended result this:
class="lang-none prettyprint-override">"13:02",42 "13:03",30 "13:04",15 "13:05",120 "13:06",10
numbers on lines timestamp origin 13:02 (so, 5 + 10 + 17 = 32 total) summed , added "13:02" column (32 + original 10 = 42), origin 13:05 gets added "13:05" column, , on.
i'm not sure how approach processing data – namely, summing numbers each minute. figuring out how info console.txt format such as
class="lang-none prettyprint-override">"13:02",32 "13:05",108 "13:06",7
would helpful, , there figure out how add together them corresponding csv rows.
thanks!
edit:thinking through process, here steps (with pseudo-code in curly brackets):
let's console.txt:
class="lang-none prettyprint-override">rate limit: 5 @ thu jun 12 13:02:00 pdt 2014 (total missed: 5) rate limit: 10 @ thu jun 12 13:02:01 pdt 2014 (total missed: 15) rate limit: 5 @ thu jun 12 13:06:34 pdt 2014 (total missed: 20)
1) reading in & cutting off unnecessary data
class="lang-none prettyprint-override">temp = open("console.txt").read() temp = temp2.replace("rate limit: ", "") temp = temp2.replace(" @ thu jun 12 ", ",") {{ remove text between "pdt 2014 (" , ")" including both of string, i.e. cutting off after seconds marker starting @ "pdt" – can myself }} {{ cutting off seconds of each min – *stuck here* }}
2) formatting
class="lang-none prettyprint-override">{{ add together quotes around times , reverse 2 columns – can figure out }}
this me:
class="lang-none prettyprint-override">"13:02",5 "13:02",10 "13:06",5
3) saving new file
class="lang-py prettyprint-override">file = open("file.txt", 'w') file.write(temp) file.close()
i can figure out adding numbers similar csv file point.
simple illustration (without reading , writing files):
csv = '''"13:02",10 "13:03",30 "13:04",15 "13:05",12 "13:06",3''' rates = '''rate limit: 5 @ thu jun 12 13:02:00 pdt 2014 (total missed: 5) rate limit: 10 @ thu jun 12 13:02:01 pdt 2014 (total missed: 15) rate limit: 17 @ thu jun 12 13:02:06 pdt 2014 (total missed: 32) rate limit: 10 @ thu jun 12 13:05:50 pdt 2014 (total missed: 42) rate limit: 14 @ thu jun 12 13:05:53 pdt 2014 (total missed: 56) rate limit: 84 @ thu jun 12 13:05:21 pdt 2014 (total missed: 140) rate limit: 2 @ thu jun 12 13:06:30 pdt 2014 (total missed: 142) rate limit: 5 @ thu jun 12 13:06:34 pdt 2014 (total missed: 147)''' # --- illustration code --- import re all_times = {} # alter csv dict x in csv.splitlines(): time, value = x.split(',') all_times[time] = int(value) # print dict print '--- old ---' k,v in all_times.items(): print k, v # add together rates dict x in rates.splitlines(): value, time = re.findall('rate limit: (\d+) .* (\d+:\d+):', x)[0] all_times['"%s"' % time] += int(value) # print dict print '--- new ---' k,v in all_times.items(): print k, v
results:
--- old --- "13:04" 15 "13:05" 12 "13:02" 10 "13:03" 30 "13:06" 3 --- new --- "13:04" 15 "13:05" 120 "13:02" 42 "13:03" 30 "13:06" 10
python sorting python-2.7 csv formatting
Comments
Post a Comment