[Python-ideas] duck typing for io write methods

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Fri Jun 14 11:33:44 CEST 2013


Chris Rebert <pyideas at ...> writes:

> 
> 
> On Jun 14, 2013 1:42 AM, "Wolfgang Maier"
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
> > Andrew Barnert <abarnert <at> ...> writes:
> > > From: Wolfgang Maier <wolfgang.maier <at> ...>
> <snip>
> > > Anyway, why do you actually want a bytes here? Maybe there's a better
> > design for what you're trying to do that
> > > would make this whole issue irrelevant to your code.
> >
> > The actual problem here is that I'm reading bytes from a text file (it's a
> > huge file, so I/O speed matters and working in text mode is no option). Then
> > I'm extracting numeric values from the file that I need for calculations, so
> > I'm converting bytes to int here. While that's fine, I then want to write
> > the result along with other parts of the original file to a new file. Now
> > the result is an integer, while the rest of the data is bytes already, so I
> > have to convert my integer to bytes to .join it with the rest, then
write it.
> > Here's the (simplified) problem:
> > an input line from my file:
> > b'somelinedescriptor\t100\t500\tmorestuffhere\n'
> > what I need is calculate the difference between the numbers (500-100), then
> > write this to a new file:
> > b'somelinedescriptor\t400\tmorestuffhere\n'
> >
> > Currently I solve this by splitting on '\t', converting elements 1 and 2 of
> > the resulting list to int, then (in slightly abstracted code)
> > b'\t'.join((element0, str(subtraction_result).encode(), element3)), then
> > writing. So, in essence, I'm going through this int -> str -> bytes
> > conversion scheme for a million lines in my file, which just doesn't feel
> > right. What's missing is a direct way for int -> bytes. Any suggestions are
> > welcome.
> http://docs.python.org/3/library/stdtypes.html#int.to_bytes
> If it was a snake it would have bit ya; Guido's time machine strikes
again; etc...
> Cheers,
> Chris
> 

Hi Chris,
this isn’t exactly what I’m looking for. The .to_bytes does a 'numeric'
conversion, but what I’m looking for is a 'character' conversion (I’m
dealing with a text file), essentially converting every digit of the int to
its ascii code, then turning these into a bytes sequence.
Still, I hadn’t come across int.to_bytes and int.from_bytes yet, and they
might be very useful at some point, so thanks a lot for this suggestion.

Best,
Wolfgang







More information about the Python-ideas mailing list