title: What is the best way for sum numbers at a big text file?
body: <p>What is the best way for sum numbers at a big text file?<br>
  The text file will contain numbers separated by a comma (',').<br>
  The number can be from any type.<br>
  No line or row limits. <br>
  for example:<br>
  1 ,-2, -3.45-7.8j ,99.6,......<br>
  ...<br>
  ...<br>
  <br>
  Input: path to the text file<br>
  Output: the sum of the numbers<br>
  <br>
  I am tried to wrote one solution at myself and want to know for better solutions:</p>
  
  <p>This is my try:
  I am working with chunks of data and not read line by line, and because the end of the chunk can contain some of the number (just -2 and not -2+3j) i am looking just on the "safe piece" the last comma (',') and the other part save for the next chunk</p>
  
  <pre><code>import re
  CHUNK_SIZE = 1017
  
  
  def calculate_sum(file_path):
      _sum = 0
      with open(file_path, 'r') as _f:
          chunk = _f.read(CHUNK_SIZE)
          while chunk:
              chunk = chunk.replace(' ', '')
              safe_piece = chunk.rfind(',')
              next_chunk = chunk[safe_piece:] if safe_piece != 0 else ''
              if safe_piece != 0:
                  chunk = chunk[:safe_piece]
              _sum += sum(map(complex, re.findall(r"[+-]\d*\.?\d*[+-]?\d*\.?\d*j|[+-]?\d+(?:\.\d+)?", chunk)))
              chunk = next_chunk + _f.read(CHUNK_SIZE)
          return _sum
  </code></pre>
  
  <p>Thanks!</p>