[Python-ideas] duck typing for io write methods
M.-A. Lemburg
mal at egenix.com
Fri Jun 14 11:36:50 CEST 2013
On 14.06.2013 10:35, Wolfgang Maier wrote:
> 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.
I think you'd be better off, reading the data as text based on
the encoding used in the data, using int() for parsing and the math,
creating text again for the output and then writing everything back
using the same encoding you used for the import, i.e.
data -> text -> math -> text -> data
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Jun 14 2013)
>>> Python Projects, Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2013-07-01: EuroPython 2013, Florence, Italy ... 17 days to go
2013-07-16: Python Meeting Duesseldorf ... 32 days to go
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list