finding the maximum value between text and numbers for each timestep
<p>I have the intention to find the maximum values for each rows under the TS in the input data for a very big data. This is the input data:</p>

<pre><code>SCALAR
ND    3
ST  0
TS        10.00
0.0000
0.0000
0.0000
SCALAR
ND    3
ST  0
TS      3600.47
255.1744
255.0201
257.0000
SCALAR
ND    3
ST  0
TS      7200.42
255.5984
255.4946
255.7014
SCALAR
ND    3
ST  0
TS      10000.0
256.5984
255.1946
255.7014
</code></pre>

<p>At the end I want to save the maximum values with the same format form different timesteps like following:</p>

<pre><code>SCALAR
ND    3
ST  0
TS      0.00
**256.60**
**255.49**
**257.00**
</code></pre>

<p>I have written a code like this:</p>

<pre><code> from __future__ import print_function

 lines = []
 Newlist = []
 with open('data.txt') as f, open('output.txt', 'w') as outfile:
     for line in f:
         lines.append(line.rstrip('\n'))
         lines1=list(enumerate(lines))
         list_n=list(zip(*(iter(lines),)*7))
         max_value = max(float(n) for n in list_n)
print(max_value, file=outfile)
</code></pre>

<p>The program works till the last line but the execution of the last line I get the following error : ValueError: max() arg is an empty sequence. I don't know why. </p>

<p>I should mention that I've deleted a lot of numbers after TS to make this example small. There is many values which has to be checked. The same line of each timesteps (TS) must be checked.</p>

<p>Your attempt fails in several different places; you assigned to <code>lines1</code> but ignored that, you try to use the <code>lines</code> list each and every iteration to produce a <code>max()</code> value, you never filtered out the non-numeric lines so trying to call <code>float()</code> on those would fail, and you never grouped the numeric lines correctly.</p>

<p>If your input file is so large, I'd not use the <code>max()</code> function but rather track the 3 maxima as you parse the file, testing each line against the maximum found so far.</p>

<p>Just read the file until you come across a <code>TS</code> line, then consume lines until there is a <code>SCALAR</code> line or the end of the file; those are numbers you want to get a maximum from which you then write out to the output file.</p>

<p>I'd preserve the format as much as possible otherwise:</p>

<pre><code>maxima = [[float('-inf'), ''] for _ in range(3)]

with open('data.txt') as f:
    for line in f:
        if line.startswith('TS'):
            # timestamp group, find maximum for the next 3 lines
            for maximum, line in zip(maxima, f):
                value = float(line)
                if value &gt; maximum[0]:
                    maximum[:] = value, line

with open('output.txt', 'w') as outfile:
    # write header to output file
    outfile.write('SCALAR\nND    3\nST  0\nTS      0.00\n')
    # write the 3 maximum lines:
    for value, line in maxima:
        outfile.write(line)
</code></pre>

<p>Note that <code>zip()</code> stops iteration as soon as one of the inputs is exhausted; by putting <code>maxima</code> first that means only 3 lines are read each time. I started the <code>maxima</code> list with <code>float('-inf')</code> because by definition, any other floating point value is going to be considered larger than that. Also, note that there is no need to strip newlines; <code>float()</code> doesn't care about leading or trailing whitespace, so any newline at the end of a line is ignored by that function.</p>

<p>The above tracks maxima as floating point values but leaves the original lines intact; the output file contains <code>256.5984</code>, <code>255.4946</code> and <code>257.0000</code> respectively, rather than rounded values.</p>

<p>This gives you output close to the original:</p>

<pre><code>&gt;&gt;&gt; from io import StringIO
&gt;&gt;&gt; sample = StringIO('''\
... SCALAR
... ND    3
... ST  0
... TS        10.00
... 0.0000
... 0.0000
... 0.0000
... SCALAR
... ND    3
... ST  0
... TS      3600.47
... 255.1744
... 255.0201
... 257.0000
... SCALAR
... ND    3
... ST  0
... TS      7200.42
... 255.5984
... 255.4946
... 255.7014
... SCALAR
... ND    3
... ST  0
... TS      10000.0
... 256.5984
... 255.1946
... 255.7014
... ''')
&gt;&gt;&gt; maxima = [[float('-inf'), ''] for _ in range(3)]
&gt;&gt;&gt; with sample as f:
...     for line in f:
...         if line.startswith('TS'):
...             # timestamp group, find maximum for the next 3 lines
...             for maximum, line in zip(maxima, f):
...                 value = float(line)
...                 if value &gt; maximum[0]:
...                     maximum[:] = value, line
...
&gt;&gt;&gt; outfile = StringIO()
&gt;&gt;&gt; outfile.write('SCALAR\nND    3\nST  0\nTS      0.00\n')
34
&gt;&gt;&gt; for value, line in maxima:
...     outfile.write(line)
...
9
9
9
&gt;&gt;&gt; print(outfile.getvalue())
SCALAR
ND    3
ST  0
TS      0.00
256.5984
255.4946
257.0000
</code></pre>

<p>You could always use <code>outfile.write('{:.2f}\n'.format(value))</code> instead, if you did want to have output rounded to 2 decimals.</p>

python list python-3.x max