Is reverse reading possible?

Anthony Liu antonyliu2002 at yahoo.com
Sun Mar 14 21:53:52 CET 2004


Hmm, it looks cool.  I actually don't know what
encoding is used for those Chinese documents.  How can
I know?

Can I do something like

file.getEncoding() 

to get its encoding?

--- Jeff Epler <jepler at unpythonic.net> wrote:
> 4M files are not that big these days.
> 
> def reversed_file(name, encoding):
>     contents = codecs.open(name, "r",
> encoding).read()
>     contents = list(contents)
>     contents.reverse()
>     return "".join(contents)
> 
> Example:
> >>> codecs.open("utf-example.txt", "r",
> "utf-8").read()
> u'60: CJK UNIFIED IDEOGRAPHS (0x4E00 - 0x9FFF)   |
> \u4e25\u5f25\u6f25\n'
> >>> reversed_file("utf-example.txt", "utf-8")
> u'\n\u6f25\u5f25\u4e25 |   )FFF9x0 - 00E4x0(
> SHPARGOEDI DEIFINU KJC :06'
> 
> This assumes that you know the encoding for each
> file in advance, and
> that Python has a codec for it.
> 
> Jeff


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com




More information about the Python-list mailing list