[Tutor] Importing data from a file.
I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? 3. Are there defined procedures for doing the required processing? With many thanks, Sydney -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On Thu, Mar 21, 2013 at 11:43 PM, Shall, Sydney sydney.sh...@kcl.ac.uk wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? To the best of my knowledge, yes to both of your queries. Once you have the element you want to process, you can make use of the type converting functions (int(), float().. ) and use them appropriately. 3. Are there defined procedures for doing the required processing? If you meant conversion functions, int() and float() are examples of those. You of course (most of the times) have to make use of string manipulation functions (strip(), rstrip(), etc) to extract the exact data item you might be looking for. So, they would be the building blocks for your processing functions. I hope that makes some things clear. -Amit. -- http://amitsaha.github.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On 21/03/2013 13:54, Amit Saha wrote: On Thu, Mar 21, 2013 at 11:43 PM, Shall, Sydney sydney.sh...@kcl.ac.uk wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? To the best of my knowledge, yes to both of your queries. Once you have the element you want to process, you can make use of the type converting functions (int(), float().. ) and use them appropriately. 3. Are there defined procedures for doing the required processing? If you meant conversion functions, int() and float() are examples of those. You of course (most of the times) have to make use of string manipulation functions (strip(), rstrip(), etc) to extract the exact data item you might be looking for. So, they would be the building blocks for your processing functions. I hope that makes some things clear. -Amit. Yes, Thanks. This is now quite clear. Sydney -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On 03/21/2013 09:43 AM, Shall, Sydney wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? No, the imported data is a module. For example import sys print type(sys) type 'module' At this point, sys is a object of type module. Perhaps you really mean data returned by the readline() method of the file object. In that case, it's a list of strings. Or data returned from the readline() method of the file object. That is a string. Or data returned from the read() method of the file object. The return type of that depends on the version of Python. Be more specific, since the answer greatly depends on how you read this data. 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? Again, you have to be specific. The program might well want exactly what one of these methods returns. 3. Are there defined procedures for doing the required processing? Sure, hundreds of thousands of them, most of them to be found in other people's programs. Sorry, but your questions are so vague as to defy definitive answers. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
3. Are there defined procedures for doing the required processing? If you meant conversion functions, int() and float() are examples of those. You of course (most of the times) have to make use of string manipulation functions (strip(), rstrip(), etc) to extract the exact data item you might be looking for. Let's add the pickle module though. Pickle converts (most) Python objects into string representations, when you unpickle these string representations you get back your object. A dictionary object becomes a dictionary object and so on. Read more here: http://docs.python.org/3.3/library/pickle.html#module-pickle -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On 03/21/2013 10:03 AM, Dave Angel wrote: A typo below; sorry. On 03/21/2013 09:43 AM, Shall, Sydney wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? No, the imported data is a module. For example import sys print type(sys) type 'module' At this point, sys is a object of type module. Perhaps you really mean data returned by the readline() method of the Perhaps you really mean data returned by the readlines() method of the file object. In that case, it's a list of strings. Or data returned from the readline() method of the file object. That is a string. Or data returned from the read() method of the file object. The return type of that depends on the version of Python. Be more specific, since the answer greatly depends on how you read this data. 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? Again, you have to be specific. The program might well want exactly what one of these methods returns. 3. Are there defined procedures for doing the required processing? Sure, hundreds of thousands of them, most of them to be found in other people's programs. Sorry, but your questions are so vague as to defy definitive answers. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On 21/03/13 13:43, Shall, Sydney wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? Assuming you mean data read from a file rather than modules imported using 'import' then the answer is 'it depends'. Most files are text files and the data is stored as strings and therefore when you read them back they will be strings. You then convert them to the native data using int(), float() etc. Some files are binary files and then the data read back will be bytes and need to be decoded into the original data. This is often done using the struct module. Either way if you use the Python read() operation on a file you will get back a bunch of bytes. What those bytes represent depends on how they were written. How they are interpreted is down to the programmer. 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? Yes, always. 3. Are there defined procedures for doing the required processing? Yes, for the standard types. For custom types and arbitrary binary data you need to find out what the original encoding was and reverse it. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing data from a file.
On 21/03/2013 16:17, Alan Gauld wrote: On 21/03/13 13:43, Shall, Sydney wrote: I have an elementary question provoked by another post today. 1. Is it the case that ALL imported data from a file is a string? Assuming you mean data read from a file rather than modules imported using 'import' then the answer is 'it depends'. Most files are text files and the data is stored as strings and therefore when you read them back they will be strings. You then convert them to the native data using int(), float() etc. Some files are binary files and then the data read back will be bytes and need to be decoded into the original data. This is often done using the struct module. Either way if you use the Python read() operation on a file you will get back a bunch of bytes. What those bytes represent depends on how they were written. How they are interpreted is down to the programmer. 2. Does this therefor imply that said data has to be processed appropriately to generate the data in the form required by the program? Yes, always. 3. Are there defined procedures for doing the required processing? Yes, for the standard types. For custom types and arbitrary binary data you need to find out what the original encoding was and reverse it. HTH, Thank you Alan, That was most useful. Cheers, Sydney -- Professor Sydney Shall, Department of Haematological Medicine, King's College London, Medical School, 123 Coldharbour Lane, LONDON SE5 9NU, Tel Fax: +44 (0)207 848 5902, E-Mail: sydney.shall, [correspondents outside the College should add; @kcl.ac.uk] www.kcl.ac.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor