Are there any Python people here?
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
At Tue, 6 Nov 2007 15:54:35 +0000, Chris G wrote:
Are there any Python people here?
Yes, loads!
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
Err, my C isn't that hot, but how do this do anything other than put all the contents of the file in str? Or does it read a line at a time in some non-obvious way?
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
You should have a look at:
http://docs.python.org/lib/bltin-file-objects.html
If you /really/ want to read in chunks of three bytes, you could do this:
chunk = tlvFile.read(3) while chunk != "": print chunk chunk = tlvFile.read(3)
Otherwise, why not just read lines and use the free iterator:
for line in tlvFile.readlines(): print line
Cheers, Richard
On Tue, Nov 06, 2007 at 04:20:05PM +0000, Richard Lewis wrote:
At Tue, 6 Nov 2007 15:54:35 +0000, Chris G wrote:
Are there any Python people here?
Yes, loads!
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
Err, my C isn't that hot, but how do this do anything other than put all the contents of the file in str? Or does it read a line at a time in some non-obvious way?
OK, the code wasn't perfect, there need to be more parameters to read() so that it only reads a bit of the file at a time.
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
You should have a look at:
http://docs.python.org/lib/bltin-file-objects.html
If you /really/ want to read in chunks of three bytes, you could do this:
chunk = tlvFile.read(3) while chunk != "": print chunk chunk = tlvFile.read(3)
That's what I was trying to avoid, those two reads.
Otherwise, why not just read lines and use the free iterator:
for line in tlvFile.readlines(): print line
Because the file doesn't have lines, it's just a stream of bytes.
On 06/11/2007, Chris G cl@isbd.net wrote:
Are there any Python people here?
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
I'm not sure I understand your c example. The c read function returns the number of bytes read or -1 on an error. It's declaration is:
ssize_t read(int fd, void *buf, size_t count);
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
That will read at most 3 bytes from the file. If you've reached EOF then it will return the empty string. The empty string returns false if evaluated in a boolean context so:
tagNum = tlvFile.read(3) while tagNum: # do stuff with tagNum tagNum = tlvFile.read(3)
would probably work. Not sure how you are intending to handle your variable record length stuff in a while loop though.
JD
On Tue, Nov 06, 2007 at 04:21:19PM +0000, Jon Dye wrote:
On 06/11/2007, Chris G cl@isbd.net wrote:
Are there any Python people here?
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
I'm not sure I understand your c example. The c read function returns the number of bytes read or -1 on an error. It's declaration is:
ssize_t read(int fd, void *buf, size_t count);
Yes, as I said before, it wasn't right, what I meant was to have the parameters as you say with a fairly small 'count'. The the loop iterates until EOF.
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
That will read at most 3 bytes from the file. If you've reached EOF then it will return the empty string. The empty string returns false if evaluated in a boolean context so:
tagNum = tlvFile.read(3) while tagNum: # do stuff with tagNum tagNum = tlvFile.read(3)
would probably work. Not sure how you are intending to handle your variable record length stuff in a while loop though.
Yes, I just don't feel those two tlvFile.read(3) calls are very elegant somehow.
On Tue, Nov 06, 2007 at 07:03:25PM +0000, Chris G wrote:
On Tue, Nov 06, 2007 at 04:21:19PM +0000, Jon Dye wrote:
On 06/11/2007, Chris G cl@isbd.net wrote:
Are there any Python people here?
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
I'm not sure I understand your c example. The c read function returns the number of bytes read or -1 on an error. It's declaration is:
ssize_t read(int fd, void *buf, size_t count);
Yes, as I said before, it wasn't right, what I meant was to have the parameters as you say with a fairly small 'count'. The the loop iterates until EOF.
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
That will read at most 3 bytes from the file. If you've reached EOF then it will return the empty string. The empty string returns false if evaluated in a boolean context so:
tagNum = tlvFile.read(3) while tagNum: # do stuff with tagNum tagNum = tlvFile.read(3)
would probably work. Not sure how you are intending to handle your variable record length stuff in a while loop though.
Yes, I just don't feel those two tlvFile.read(3) calls are very elegant somehow.
If elegance is the issue, objects are perhaps a solution; you can write yourself a little class that allows you to iterate over the file using a for loop:
class FileSequence(object) :
def __init__(self, f) : self.f = f
def __iter__(self) : return self
def next(self) : s = self.f.read(3) if s == '' : raise StopIteration return s
fs = FileSequence(file(sys.argv[1], 'r')) for w in fs : print w
Obviously, the next method can read and return chunks of variable length and instance variables can be used for any state required to work out chunk lengths or perhaps to buffer stuff... so this might even be useful in addition to being elegant... in fact, pretty neat lexical scanners can be written this way.
Best regards, Jan
On Tue, Nov 06, 2007 at 07:44:53PM +0000, Jan T. Kim wrote:
tagNum = tlvFile.read(3) while tagNum: # do stuff with tagNum tagNum = tlvFile.read(3)
would probably work. Not sure how you are intending to handle your variable record length stuff in a while loop though.
Yes, I just don't feel those two tlvFile.read(3) calls are very elegant somehow.
If elegance is the issue, objects are perhaps a solution; you can write yourself a little class that allows you to iterate over the file using a for loop:
class FileSequence(object) :
def __init__(self, f) : self.f = f
def __iter__(self) : return self
def next(self) : s = self.f.read(3) if s == '' : raise StopIteration return s
fs = FileSequence(file(sys.argv[1], 'r')) for w in fs : print w
Obviously, the next method can read and return chunks of variable length and instance variables can be used for any state required to work out chunk lengths or perhaps to buffer stuff... so this might even be useful in addition to being elegant... in fact, pretty neat lexical scanners can be written this way.
Now that's the sort of solution I was after, thank you! :-)
Hi
On 06/11/2007, Chris G cl@isbd.net wrote:
Are there any Python people here?
Not me. Without semi-colons at the end of the line, it just reminds me of the time I used to program in VB. Yuck!
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL)
Which read() is that??
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
Have you Google-searched for "python EOF file"? Generally there will be some way to know if EOF has been reached. If there's no support, ditch the language ;)
Good luck
Srdjan
On Tue, Nov 06, 2007 at 03:54:35PM +0000, Chris G wrote:
Are there any Python people here?
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
How big a file are you talking? Why are you reading in chunks? And the C example doesn't show you reading in chunks (well, except I'm assuming that file in that instance is actually not a real file persay, but a file like object that will return data when it has it)...
Because you can do a straight read of all of the data using python using: data = fh.read()
Which would put the entire content of the file opened with filehandle fh in to data, then you can slice and dice as you like...
So, rather than give us a (very bad) C example, how about actually giving us some idea of what you're *actually* trying to do... even better with some example data file...
Believe it or not, we are not mind readers, and we can not help those that don't want it.
Thanks,