[Tutor] pass tuples to user defined function(beginner)
Alan Gauld
alan.gauld at btinternet.com
Wed Nov 30 02:16:14 CET 2011
On 29/11/11 21:40, Mayo Adams wrote:
> tuple that is in the file, it would be helpful to know what it is.
> Presumably, a string representing a tuple.
Exactly so, everything in the file is a string. If it's a binary file it
is a string of bytes, if it is a text file a string of characters. The
interpretation of those bytes or characters is down to the reading
program. The program must convert the string into data. The string
represents the data that will be eventually created in the computer's
memory within the interpreter's execution environment.
> representation, I cant immediately see how anything in a script is
> anything other than a representation of some kind, hence the
> distinction between representamen and object does no work for me.
While it is in the file it is just a string. a string representation of
a program. The interpreter reads that string and converts it into symbol
tables, executable objects and data structures. Your program, when it
reads a file must do exactly the same, it must convert the string read
from the file into whatever data structure it represents and, if
necessary, assign that data to a variable for future reference.
The process of reading a string and interpreting its meaning as data is
what's known as parsing. There are several parsers available in Python
to help with that task, as well as some tools to help you write your own
parser. Examples are the csv module, HTMLParser, xpat and ElementTree.
But even with these you will often have to do the final type conversion
from string to integer or Boolean etc yourself.
The other option is to use binary files where the data is stored in
strings of bytes. In this case you still need to interpret the string
but you do so by reading the exact number of bytes needed to store the
particular data type, for example 4 bytes for a 32 bit integer. You then
have to convert those 4 bytes into an integer. Again Python offers some
help in the form of the struct module. Binary files are usually more
compact than text files storing the same information, but they are far
harder to interpret. There are other standard types of binary file and
Python offers support for some, most notably there are libraries to read
Excel files, various databases and PDF documents. Most of these
libraries will also contain functions that help you write data back out
to the files in the same format as well.
The goal of all of these libraries is to make it comparatively easy to
read a string representation from a file and convert it into real data
that we can manipulate in our programs.
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list