title: How do you dynamically identify unknown delimiters in a data file?
body: <p>I have three input data files. Each uses a different delimiter for the data contained therein. Data file one looks like this:<br/></p>
  
  <pre>apples | bananas | oranges | grapes</pre>
  
  <p>data file two looks like this:<br/></p>
  
  <pre>quarter, dime, nickel, penny</pre>
  
  <p>data file three looks like this:<br/></p>
  
  <pre>horse cow pig chicken goat</pre>
  
  <p>(the change in the number of columns is also intentional)<br/></p>
  
  <p>The thought I had was to count the number of non-alpha characters, and presume that the highest count was the separator character. However, the files with non-space separators also have spaces before and after the separators, so the spaces win on all three files. Here's my code:</p>
  
     <pre><code>def count_chars(s):
      valid_seps=[' ','|',',',';','\t']
      cnt = {}
      for c in s:
          if c in valid_seps: cnt[c] = cnt.get(c,0) + 1
      return cnt
  
  infile = 'pipe.txt' #or 'comma.txt' or 'space.txt'
  records = open(infile,'r').read()
  print count_chars(records)</code></pre>
  
  <p>It will print a dictionary with the counts of all the acceptable characters. In each case, the space always wins, so I can't rely on that to tell me what the separator is.<br/></p>
  
  <p>But I can't think of a better way to do this. <br/></p>
  
  <p>Any suggestions?</p>