Python dos2unix one liner

@ Rocteur CC macosx at rocteur.cc
Sat Feb 27 10:36:41 CET 2010


Hi,

This morning I am working though Building Skills in Python and was  
having problems with string.strip.

Then I found the input file I was using was in DOS format and I  
thought it be best to convert it to UNIX and so I started to type perl  
-i -pe 's/ and then I though, wait, I'm learning Python, I have to  
think in Python, as I'm a Python newbie I fired up Google and typed:

+python convert dos to unix +one +liner

Found perl, sed, awk but no python on the first page

So I tried

+python dos2unix +one +liner -perl

Same thing..

But then I found http://wiki.python.org/moin/Powerful%20Python%20One-Liners 
  and tried this:

cat file.dos | python -c "import sys,re; 
[sys.stdout.write(re.compile('\r\n').sub('\n', line)) for line in  
sys.stdin]" >file.unix

And it works..

[10:31:11 incc-imac-intel ~/python] cat -vet file.dos
one^M$
two^M$
three^M$
[10:32:10 incc-imac-intel ~/python] cat -vet file.unix
one$
two$
three$

But it is long and just like sed does not do it in place.

Is there a better way in Python or is this kind of thing best done in  
Perl ?

Thanks,

Jerry



More information about the Python-list mailing list