If I understood correctly what you want, here's a code that keeps the format of the lines:
text = """DATA gfghsg hsghghsfghsfghshsdhf
1 253.31 78.20 490.0 0 0 1 0 0
101 .0 0 0 0 0 0
1 2 3 4 5 6
2 123.31 122.20 -20.0 0 0 1 0 0
201 0 0 0 0 0 0
7 8 9 10 11 12
6 6 . 66 666 4 8 7 4 5 7
3 53.21 10.2 90.0e+15 0 0 1 0 0
301 0 0 0 0 0 0
13 14 15 11 10 10
kjqbskjqskdkqsdbkjqsbd
547 AFFO 457 6545 1 0 2 5 4
10 44 138 -.017 0 0 1 0 0
410 0 0 0 0 0 0
20 21 22 23 24 25
8888 9999
500 87E-458 12 .4
1.2 4.E-56
12 45 """
.
import re,csv
pat = '^([ \t]*[-+]?(?:\d+\.?|\.?\d)[\deE+-]*)'\
'([ \t]+[-+]?(?:\d+\.?|\.?\d)[\deE+-]*)'\
'([ \t]+[-+]?(?:\d+\.?|\.?\d)[\deE+-]*)'\
'([ \t]+[-+]?(?:\d+\.?|\.?\d)[\deE+-]*)'\
'([ \t]*(?:[-+]?(?:\d+\.?|\.?\d)[\deE+-]*[ \t]*)*\n'\
\
'^[ \t]*(?:[-+]?(?:\d+\.?|\.?\d)[\deE+-]*[ \t]*)+\n'\
\
'^[ \t]*(?:[-+]?(?:\d+\.?|\.?\d)[\deE+-]*[ \t]*)+)$'
r = re.compile(pat,re.MULTILINE)
def modify(text,filepath,r = r):
with open(filepath,'rb') as vava:
VALUES = map(tuple,
csv.reader(vava, delimiter='\t', skipinitialspace=True))
dic = {}
def ripl(m,VALUES=VALUES,dic=dic):
lens = tuple(len(x) for x in m.group(2,3,4))
pat = dic.setdefault(lens,'%%%ds%%%ds%%%ds' % lens)
return m.group(1) + pat % VALUES.pop(0) + m.group(5)
return r.sub(ripl,text)
print modify(text,'values.csv')
result
DATA gfghsg hsghghsfghsfghshsdhf
1 100000 0.01 101.01 0 0 1 0 0
101 .0 0 0 0 0 0
1 2 3 4 5 6
2 2 0.02 20022 0 0 1 0 0
201 0 0 0 0 0 0
7 8 9 10 11 12
6 6 . 66 666 4 8 7 4 5 7
3 3303 0.033 3.03333 0 0 1 0 0
301 0 0 0 0 0 0
13 14 15 11 10 10
kjqbskjqskdkqsdbkjqsbd
547 AFFO 457 6545 1 0 2 5 4
10 4.4 0.44 4.4 0 0 1 0 0
410 0 0 0 0 0 0
20 21 22 23 24 25
8888 9999
500 5555 0.5555555e+55
1.2 4.E-56
12 45
Teh part
lens = tuple(len(x) for x in m.group(2,3,4))
pat = dic.setdefault(lens,'%%%ds%%%ds%%%ds' % lens)
is a sophistication that takes account of the possibility that the format wouldn't be always the same for all the modified lines. So it examines the lengthes of the 4 first parts of a line containing the 4 first values: if these values are already known, the corresponding pattern is got from the dictionary dic
, and if not the new pattern is created and put in the dictionary.