Considering migrating to Python from Visual Basic 6 for engineering applications
bc at freeuk.com
Mon Feb 22 07:24:05 EST 2016
On 22/02/2016 08:50, Jussi Piitulainen wrote:
> BartC writes:
>> Reading stuff from an interactive console or terminal, is such an
>> important part of the history of computing, still being used now, that
>> you'd think a language would provide simple, ready-to-use methods ways
>> to do it.
> I think it does. Do you at least agree that it provides a reasonably
> simple way to ask for a single line of text?
> stuff = input()
> Is that already too obscure or advanced for you? Because surely not?
That's a good starting point. It reads an entire line of text, as a
string. So that's part of the job done. The rest of it needs some string
But even 'input' has some issues. While it seemed to work, the docs I
looked at suggested it interpreted the input as an expression, and I
needed to use raw_input().
Was this a Python 2/3 problem? The docs I used
didn't appear to mention that, not in the title or around the edges
anyway! Eventually I saw that as a italicised note within the text.
So, it's input() on 3.x and raw_input() on 2.x.
But another thing is that it doesn't scale up to read from a file, as
far as I can see. File reading is rather different, and it behaves
differently regarding line endings.
(For comparison, here's how it works on another language I use:
This waits for a line of input (it becomes the read buffer), then reads
three integers from the buffer.
This reads two more integers from that buffer (if there is no more data,
it reads zeros).
This does the same, from file handle f.
The equivalent of Python's input() might be:
s := sreadln() # from console
s := sreadln(f) # from a file
s := sreadln("10,20") # from a string
but the entire line is also placed into the read buffer so that
subsequent read commands (or sread() function calls) can read individual
values as before.
Reading anything other than integers is bit more fiddly (you can't have
everything!) So that means using something like this:
read a:"r", b:"h", c:"s", d:"n"
So reading as a float, an int using hexadecimal, a string (delimited by
white space or punctuation, or quoted), a name (file names etc), and so on.
There are limitations, but this stuff is there when I don't need
anything more sophisticated.)
More information about the Python-list