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