jrlen balane said unto the world upon 2005-02-13 11:49:
guys, how would i do this: i want to read from a text file
the text file should contain should contain data (say, decimal value
from 1-1200). there should be no other type of entry but decimal
it should contain 96 data all in all, with each data separated by a
comma or each data is numbered from 1-96 (or better yet, any
suggestions on this???)

Hi,

So, you have control over the creation of the data file, too? Is it being created by a Python process? If so, pickling your data might make more sense. If the data was generated by a class with each data point stored as a class attribute, for instance, you could just pickle the class. Does this sound possible in your situation?

now, the program should read each data every 5 minutes for eight hours
the data will be sent to the serial port and will be executed by an
external hardware. the serial part is ok, and the problem now is how
to read from a text file.
any help, pls.

what i know (chapter 7 of the tutorial):
1) first, to open a txt file, i can use open() say:
                f = open(*.txt, r)
a user can use the notepad to create the text file so i'll just open
it for reading. my problem now would be on reading the contents of the
file.
2) f.read(size), since my data ranges from 0-1200, size = 2 for each read.
3) should i do this:
       for data in range (1, 96,1):
            f.read(2)
            ...
            time.sleep(300) # 5 minutes
4) since f.read() returns string, how would i convert this back to
decimal value, since i would need the decimal value for the serial
part.

Since you files are quite short, I'd do something like:

<code>
data_file = open(thedata.txt, 'r') # note -- 'r' not r
data = data_file.readlines()       # returns a list of lines

def process(list_of_lines):
    data_points = []
    for line in list_of_lines:
        data_points.append(int(line))
    return data_points

process(data)
</code>

This assumes that each line of the data file has nothing but a string with an int followed by '\n' (for end of line), and that all you need is a list of those integers. Maybe these are bad assumptions -- but they might get you started.

HTH,

Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to