ascii to unicode line endings

fidtz at clara.co.uk fidtz at clara.co.uk
Thu May 3 08:54:29 EDT 2007


On 3 May, 13:39, "Jerry Hill" <malaclyp... at gmail.com> wrote:
> On 2 May 2007 09:19:25 -0700, f... at clara.co.uk <f... at clara.co.uk> wrote:
>
> > The code:
>
> > import codecs
>
> > udlASCII = file("c:\\temp\\CSVDB.udl",'r')
> > udlUNI = codecs.open("c:\\temp\\CSVDB2.udl",'w',"utf_16")
> > udlUNI.write(udlASCII.read())
> > udlUNI.close()
> > udlASCII.close()
>
> > This doesn't seem to generate the correct line endings. Instead of
> > converting 0x0D/0x0A to 0x0D/0x00/0x0A/0x00, it leaves it as  0x0D/
> > 0x0A
>
> That code (using my own local files, of course) basically works for me.
>
> If I open my input file with mode 'r', as you did above, my '\r\n'
> pairs get transformed to '\n' when I read them in and are written to
> my output file as 0x00 0x0A.  If I open the input file in binary mode
> 'rb' then my output file shows the expected sequence of 0x00 0x0D 0x00
> 0x0A.
>
> Perhaps there's a quirk of your version of python or your platform?  I'm running
> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
> (Intel)] on win32
>
> --
> Jerry

Thanks very much! Not sure if you intended to fix my whole problem,
but changing the read mode to 'rb' has done the trick :)

Dom




More information about the Python-list mailing list