[Python-Dev] Docs for string methods ?
M.-A. Lemburg
mal@lemburg.com
Wed, 05 Jul 2000 12:29:11 +0200
Fredrik Lundh wrote:
>
> mal wrote:
> > Fredrik Lundh wrote:
> > >
> > > mal wrote:
> > > > I've just checked in new string methods
> > >
> > > talking about string methods: how about providing an
> > > "encode" method for 8-bit strings too?
> >
> > I've tossed that idea around a few times too... it could
> > have the same interface as the Unicode one (without default
> > encoding though). The only problem is that there are currently
> > no codecs which could deal with strings on input.
>
> imho, a consistent interface is more important than a truly
> optimal implementation (string are strings yada yada). or in
> other words,
>
> def encode(self, encoding):
> if encoding is close enough:
> return self
> return unicode(self).encode(encoding)
>
> ought to be good enough for now.
Ideally str() should have an encoding parameter too. Then I
could add generic PyString_Encode|Decode|AsEncodedString
APIs to stringobject.c which match the corresponding ones
in unicodeobject.c
gzippeddata = file.read()
# Unzip a data packet (decode part of the codec)
data = str(gzippeddata, 'data_gzip')
# Zip a data packet (encode part of the codec)
gzippeddata = data.encode('data_gzip')
Looks cool ;-)
Note that 'abc'.encode('utf8') would fail because the UTF-8
codec expects Unicod on input to its encode method (hmm, perhaps
I ought to make the method use the 'u' parser marker instead
of 'U' -- that way, the method would auto-convert the 'abc'
string to Unicode using the default encoding and then proceed
to encode it in UTF-8).
Other opinions ?
--
Marc-Andre Lemburg
______________________________________________________________________
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/