Considering migrating to Python from Visual Basic 6 for engineering applications
Larry Hudson
orgnut at yahoo.com
Sat Feb 20 02:49:06 EST 2016
On 02/19/2016 10:14 AM, wrong.address.1 at gmail.com wrote:
[snip]
>
> This is precisely reading one character at a time. If not exactly reading one character, it is effectively looking at each character to assemble the number. Not a good sign. I guess there might be libraries which will help read numbers better, but I would expect a good language to be able to handle this basic thing for engineers - to read numbers like Fortran and Basic do.
>
> Still, if I have misunderstood something, I will be glad to be corrected. I would generally know that the first three numbers will be floating point, the next will be something and then the next will be something else, etc. Does that help or does one still have to look at each character and determine how to place it in a number?
>
> Thanks for your patience. I don't want surprises later on, which is why I am asking very stupid questions.
>
It absolutely does NOT require reading a character at a time! You are reading the data from a
text file, which means everything is a string. These strings (or sub-strings) can represent
integers, floats, dates, phone numbers or whatever. Your example data implies a free-form
style, where it is then necessary to determine the data type for each of these (sub)strings
individually. Of course, if your data has a defined fixed format, this is simplified -- but
they are still initially strings that have to be converted. String processing in Python is very
powerful and versatile.
BTW, it does no good to continue to think strictly in BASIC techniques. Python is a different
language with a different approach to attacking problems. If you try to write BASIC (or C or
Java or ...) programs in Python syntax you'll just get bad programs. Forget trying to find
features in Python that are identical to the features of BASIC. Python requires a different
mind-set to use it properly -- just like any other language.
Here is a rather naive somewhat brute-force way to read your example data. I'm using a Python
list here to simulate the file reading. (Actually, using read() instead of readline() will also
give this list.) Python lists are essentially the same as arrays in other languages, but much
more powerful and versatile. Two examples of the differences between arrays and lists are: can
use mixed data types, you are not restricted to all the same data type, and the size of lists
are dynamic -- they grow or shrink as necessary.
Comments start with a #
Strings in Python can be delimited by single-quotes ('), double-quotes (") or triple-quotes ("""
or '''). Triple-quoted strings can be multi-line text. The triple-quote here is used as what
Python calls a docstring, which it saves internally for documenting your program.
<code>
# Define a function to determine the data type a string represents
def chktyp(s):
"""Check string s for int, float or string. Returns the data and a type code"""
try:
return int(s), 'int' # Try to convert to int, return it if successful
except ValueError: # No, it's not an int
pass # Drop into float test
try:
return float(s), 'float' # Try to convert to float, return it if successful
except ValueError: # No, it's not a float
return s, 'str' # It must be a string
# Here is your (simulated) data as a list of strings
data = [
'2 12.657823 0.1823467E-04 114 0',
'3 4 5 9 11',
'"Lower"', # Could be left simply as "Lower"
'278.15']
# Process the data
for line in data: # For each line of the data
dat = line.split() # Make a list of the sub-strings in the line
for stng in dat: # For each substring
dt, tp = chktyp(stng) # Get the data and the type
print('{} is a {}'.format(dt, tp)) # Print this data
</code>
Running this example gives this result:
2 is a int
12.657823 is a float
1.823467e-05 is a float
114 is a int
0 is a int
3 is a int
4 is a int
5 is a int
9 is a int
11 is a int
"Lower" is a str
278.15 is a float
I hope this gives you a slight hint about the Python way of doing things. Yes, of course it's
very different from BASIC, but if you can pick up on the Pythonic way of doing things I think
you might at least find it useful.
-=- Larry -=-
More information about the Python-list
mailing list