[Tutor] Reading in data files

Marc Tompkins marc.tompkins at gmail.com
Mon Mar 14 20:57:24 CET 2011


On Mon, Mar 14, 2011 at 12:08 PM, paige crawford
<plcrawford at crimson.ua.edu>wrote:

> Hey,
>
>
> How do I read in a data file that has comments then remove the comments to
> get to the numbers? The numbers in the data file needs to be read into a
> list. There are also comments next to the numbers. The file looks something
> like this
>
> # DO NOT MODIFY
> #
> # This file is for the eyebrows
> #
> #  Read in these numbers
> #
> #
> #
>
> 3 # This is the amount of items in each list, m
>
> 30        #  number 1
> 10        # number 3
> -110     # number 9
>
> 39 40 30
>
> 48 390  # extra data
>
>
Those lines with multiple numbers (e.g. "39 40 30") - do you want to add 39,
40, and 30 to the list, or "39 40 30"?

If you want to add the whole line as a unit, then just read each line,
split() at the #, strip() the first part, and add it to the list.

If you want them to be separate numbers, then read each line, split() at the
#, split() the first part by spaces, and add each item to the list.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110314/d95e8f24/attachment.html>


More information about the Tutor mailing list