Considering migrating to Python from Visual Basic 6 for engineering applications

Tim Chase python.list at tim.thechases.com
Fri Feb 19 08:40:57 EST 2016


On 2016-02-19 02:47, wrong.address.1 at gmail.com wrote:
> 2 12.657823 0.1823467E-04 114 0
> 3 4 5 9 11
> "Lower"
> 278.15
> 
> Is it straightforward to read this, or does one have to read one
> character at a time and then figure out what the numbers are? -- 

It's easy to read.  What you do with that mess of data is the complex
part.  They come in as byte-strings, but you'd have to convert them
to the corresponding formats:

  from shlex import shlex
  USE_LEX = True # False
  with open('data.txt') as f:
    for i, line in enumerate(f, 1):
      if USE_LEX:
        bits = shlex(line)
      else:
        bits = line.split()
      for j, bit in enumerate(bits, 1):
        if bit.isdigit():
          result = int(bit)
          t = "an int"
        elif '"' in bit:
          result = bit
          t = "a string"
        else:
          result = float(bit)
          t = "a float"
        print("On line %i I think that item %i %r is %s: %r" % (
          i,
          j,
          bit,
          t,
          result,
          ))

The USE_LEX controls whether the example code uses string-splitting
on white-space, or uses the built-in "shlex" module to parse for
quoted strings that might contain a space.  The naive way of
string-splitting will be faster, but choke on string-data containing
spaces.

You'd have to make up your own heuristics for determining what type
each data "bit" is, parsing it out (with int(), float() or whatever),
but the above gives you some rough ideas with at least one known
bug/edge-case.  But we can't do *all* the work for you ;-)

-tkc







More information about the Python-list mailing list