[Tutor] convert a file from plaintext(Ascii) to unicode? very quickquestions

luke rabidpoobear at gmail.com
Tue Aug 16 03:51:02 CEST 2005


List:
I'm forwarding this private message(hope you don't mind Denise)
I personally have no idea what to do, but
someone else might be able to help.
-Luke


----- Forwarded Message -----

text is a list, so you can't encode it.  but you can iterate over each
of the elements and encode them.  I have tried several variations of
that, but keep ending up with all my newlines being little boxes. any
ideas?

Thanks,
Denise

On 8/15/05, luke <rabidpoobear at gmail.com> wrote:
> um...
> >>> help(unicode)
> Help on class unicode in module __builtin__:
> [snip help stuff]
>  |  encode(...)
>  |      S.encode([encoding[,errors]]) -> string or unicode
>  |
>  |      Encodes S using the codec registered for encoding. encoding
defaults
>  |      to the default encoding. errors may be given to set a different
> error
>  |      handling scheme. Default is 'strict' meaning that encoding errors
> raise
>  |      a UnicodeEncodeError. Other possible values are 'ignore',
'replace'
> and
>  |      'xmlcharrefreplace' as well as any other name registered with
>  |      codecs.register_error that can handle UnicodeEncodeErrors.
>
> [snip rest of help]
>
> Denise,
> I dont know much about Unicode but it seems like
> f = file(filename, "r")
> text = f.readlines()
> text = text.encode()
> #or maybe just text.encode()?
> f.close()
>
> should encode the filetext to unicode.
> then you could do a
> f = file(filename, "w")
> f.writelines(text)
> f.close()
>
> sorry I don't know more about it but I hope that helps you.
> -Luke
> ----- Original Message -----
> From: "D. Hartley" <denise.hartley at gmail.com>
> To: "Python tutor" <tutor at python.org>
> Sent: Monday, August 15, 2005 7:49 PM
> Subject: [Tutor] convert a file from plaintext(Ascii) to unicode? very
> quickquestions
>
>
> > Hello guys!
> >
> > Is there a way to convert a file from plaintext(Ascii) to unicode?  I
> > have found all my notes about moving between hex., int's, chr's,
> > ord's, etc, but all of these things were only riddle exercises to me
> > and I have a hard time keeping them straight.  I'm pretty sure I knew
> > the answer to this and now cannot find it anywhere (even googling
> > through my old tutor posts!)
> >
> > Does anyone have a quick answer/pointer?
> >
> > Thanks so much!
> >
> > ~Denise
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



More information about the Tutor mailing list