[Python-ideas] duck typing for io write methods
Chris Rebert
pyideas at rebertia.com
Fri Jun 14 11:10:52 CEST 2013
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130614/efa37dad/attachment.html>
More information about the Python-ideas
mailing list