[python-win32] convert encoding from UNIX/MAC to DOS

Tim Roberts timr at probo.com
Thu May 26 20:25:38 CEST 2005


On Wed, 25 May 2005 21:15:42 +0800 (CST), yuan ye 
<dohope2001 at yahoo.com.cn> wrote:

>Hello everyone,
>If you have ever used UltraEdit, you will know that it
>can convert encoding from Unix/Mac to Dos. Does
>anybody know how I can do it in Python without the
>help of UltraEdit? For example, to convert the
>encoding from Unix/Mac to Dos for a TXT file.
>


I think this is not a proper use of the word "encoding".  There is no 
such thing as a "DOS encoding" or a "Unix/Mac encoding".

Instead, I rather suspect that this is translating the end-of-line 
characters, which ARE different between the three environments.  Unix 
marks end-of-line with a single linefeed (0x0A).  The Mac marks 
end-of-line with a single carriage return (0x0D).  DOS uses both (0x0D, 
0x0A).

If you have Python 2.3 or greater, it allows you to open a file for 
"universal newlines", where it will detect the standard in use and 
handle it appropriately.  So, for short files, you can do this:

  open('localEndings.txt','w').write( 
open('unknownEndings.txt','rU').read() )

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list