Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Printing unix Line endings from Windows.

Reply
Thread Tools

Printing unix Line endings from Windows.

 
 
Ant
Guest
Posts: n/a
 
      12-04-2006
Hi all,

I've got a problem here which has me stumped. I've got a python script
which does some text processing on some files and writes it back out to
the same file using the fileinput module with inplace set to True.

The script needs to run from Windows, but the files need to be written
with Unix line endings.

Is there any way of doing this without having to post-process the file
in binary mode (a-la the crlf.py script)

Cheers,

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      12-04-2006
Ant wrote:
> Hi all,
>
> I've got a problem here which has me stumped. I've got a python script
> which does some text processing on some files and writes it back out to
> the same file using the fileinput module with inplace set to True.
>
> The script needs to run from Windows, but the files need to be written
> with Unix line endings.
>
> Is there any way of doing this without having to post-process the file
> in binary mode (a-la the crlf.py script)
>
> Cheers,
>

You can write to a new file and create your own line endings.
When done, delete the original file and rename the output file.

-Larry
 
Reply With Quote
 
 
 
 
Ant
Guest
Posts: n/a
 
      12-05-2006

Larry Bates wrote:

> Ant wrote:

....
> > Is there any way of doing this without having to post-process the file
> > in binary mode (a-la the crlf.py script)

....
> You can write to a new file and create your own line endings.
> When done, delete the original file and rename the output file.


How can I create my own line endings? I've tried setting os.linesep =
"\n", (and to \x0a). I've tried things like:

print "xxx yyy \n",
print "xxx uuu \x0a",
filehandle.write("xxx \n")
filehandle.write("xxx \x0a")

and all of these give me a nice windows-style crlf!

Surely there must be a way to do this ...

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-05-2006
Ant wrote:

> How can I create my own line endings? I've tried setting os.linesep =
> "\n", (and to \x0a). I've tried things like:
>
> print "xxx yyy \n",
> print "xxx uuu \x0a",
> filehandle.write("xxx \n")
> filehandle.write("xxx \x0a")
>
> and all of these give me a nice windows-style crlf!
>
> Surely there must be a way to do this ...


endline normalization is done by the file object, on the way out. to
switch this off, open the output file in binary mode ("wb").

</F>

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-05-2006

Ant wrote:
> Larry Bates wrote:
>
> > Ant wrote:

> ...
> > > Is there any way of doing this without having to post-process the file
> > > in binary mode (a-la the crlf.py script)

> ...
> > You can write to a new file and create your own line endings.
> > When done, delete the original file and rename the output file.

>
> How can I create my own line endings? I've tried setting os.linesep =
> "\n", (and to \x0a). I've tried things like:
>
> print "xxx yyy \n",
> print "xxx uuu \x0a",
> filehandle.write("xxx \n")
> filehandle.write("xxx \x0a")
>
> and all of these give me a nice windows-style crlf!
>
> Surely there must be a way to do this ...


and there is: open your output file in binary mode; then it won't
convert every \n to \r\n.

writing:
| >>> f = open('unixlf.txt', 'wb')
| >>> f.write('foo\n')
| >>> f.write('bar\n')
| >>> f.close()

checking:
| >>> f = open('unixlf.txt', 'rb')
| >>> x = f.read()
| >>> x
| 'foo\nbar\n'
| >>> len(x)
| 8

BTW:
| >>> '\n' is '\x0a'
| True

HTH,
John

 
Reply With Quote
 
Ant
Guest
Posts: n/a
 
      12-05-2006

John Machin wrote:

> Ant wrote:

....
> > filehandle.write("xxx \n")
> > filehandle.write("xxx \x0a")
> >
> > and all of these give me a nice windows-style crlf!
> >
> > Surely there must be a way to do this ...

>
> and there is: open your output file in binary mode; then it won't
> convert every \n to \r\n.

....
> | >>> '\n' is '\x0a'
> | True


(and F wrote something very similar.)

Cheers guys. Shame that fileinput doesn't take an argument to specify
the write mode when 'inplace' is True, because it is otherwise makes
editing multiple files in place very simple.

Is it worth me submitting a patch to fileinput which can take an
optional write mode parameter?

Cheers,

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-05-2006
Ant wrote:

> Is it worth me submitting a patch to fileinput which can take an
> optional write mode parameter?


absolutely.

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
module: zipfile.writestr - line endings issue towers Python 7 08-17-2007 04:40 PM
ascii to unicode line endings fidtz@clara.co.uk Python 5 05-03-2007 02:36 PM
Detecting line endings Fuzzyman Python 18 02-08-2006 12:10 PM
UTF16, BOM, and Windows Line endings Fuzzyman Python 4 02-07-2006 09:23 AM
Active State Perl with Unix-line endings -- Help Brian & Colleen Perl Misc 1 10-16-2004 09:43 PM



Advertisments