Roujri Roujri - 3 months ago 8
Python Question

Read block Text of a text file python

Input File - input.csv

#######A Result:#########
2016-07-27 bar 51 14
2015-06-27 roujri 30 86
#######B Result:#########
2016-08-26 foo 34 83
2016-08-26 foo 34 83
#########################


Output result

A result:
Col-1: 81
Col-2: 100
B result:
Col-1: 68
Col-2: 166


I am trying to solve one problem according to above input, output. So far I can read only first block Text. I want to more generic function so possibly I will only initialise the variable that need to read within the block, not hard coding (e.g.
#######A Result:#########
) and furthermore pass the block info to another function that will sum up the value. Any suggestion will be greatly appreciate. Thanks :)

import re
def reading_block_text_file(infile):
with open(infile) as fp:
for result in re.findall('#######A Result:#########(.*?)#######B Result:#########', fp.read(), re.S):
print result,

reading_block_text_file(input_file)

Answer

Throw in a little bit of regex:

$ cat a
#######A Result:#########
2016-07-27   bar       51     14
2015-06-27   roujri    30     86
#######B Result:#########
2016-08-26   foo       34      83
2016-08-26   foo       34      83
#########################
$ cat a.py
import re
col_names = ['abc', 'xyz']
with open("/tmp/a", "r") as f:
    tables = re.findall(r'#+(\w+ Result:)#+([^#]*)', f.read(), re.S)
    for table in tables:
        name = table[0]
        rows = table[1].strip().split('\n')
        print name
        for i in range(len(col_names)):
            print "\t{}: {}".format(col_names[i], sum(map(lambda x: int(x.split()[i + 2]), rows)))
$ python a.py
A Result:
    abc: 81
    xyz: 100
B Result:
    abc: 68
    xyz: 166

Regex Explanation:

#+(\w+ Result:)#+([^#]*)

Regular expression visualization

Debuggex Demo

Comments