[Tutor] convert a file from plaintext(Ascii) to unicode? very quickquestions
Michael Lange
klappnase at freenet.de
Tue Aug 16 13:13:34 CEST 2005
On Mon, 15 Aug 2005 22:51:20 -0400
Kent Johnson <kent37 at tds.net> wrote:
> I think Luke's suggestion will work if you use f.read() (to read the whole file as a single string) instead of f.readlines() and f.write() instead of writelines().
>
> Kent
>
And if you want to convert ascii into unicode you need to call * decode() * ( which does pretty much the same as unicode() )
on the string, not encode() .
Michael
> luke wrote:
> > 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:
> >>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()
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list