On Tue, 2009-01-13 at 12:02 -0800, Santiago Romero wrote: > Hi. > > Until now, all my python programs worked with text files. But now I'm > porting an small old C program I wrote lot of years ago to python and > I'm having problems with datatypes (I think). > > some C code: > > fp = fopen( file, "rb"); > while !feof(fp) > { > value = fgetc(fp); > printf("%d", value ); > } > > I started writing: > > fp = open(file, "rb") > data = fp.read() > for i in data: > print "%d, " % (int(i)) > > But it complains about i not being an integer... . len(data) shows > exactly the file size, so maybe is a "type cast" problem... :-? >
int() expects something that "looks like" an integer. E.g. int(2) => 2 int(2.0) => 2 int('2') => 2 int('c') => ValueError If you are reading arbitrary bytes then it will likely not always "look" like integers. What you probably meant is: for i in data: print "%d, " % ord(i) But if you are really dealing with C-like data structures then you might be better off using the struct module. -a -- http://mail.python.org/mailman/listinfo/python-list