Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Reading a DOS text file and writing out Mac (http://www.velocityreviews.com/forums/t319561-re-reading-a-dos-text-file-and-writing-out-mac.html)

Skip Montanaro 07-11-2003 06:05 PM

Re: Reading a DOS text file and writing out Mac
 

Jen> Is there a way to set the EOL character that Python recognizes?

Not really.

Jen> For example, I'd like to set it to cr/lf (okay, that's eol
Jen> characters) when reading a file (DOS text) and set it to just cr
Jen> when writing out (Mac). Is there a way to do this?

Sure. Open the input file in Universal newline mode (add "U" to the open
flags) then write it in binary mode, appending the '\r' character yourself.
Universal newline mode is new in 2.3 however. You can always open the input
file in binary mode and explicitly strip any trailing \r\n pairs.

inf = open("somedosfile", "rb")
outf = open("somemacfile", "wb")
for line in inf.read().split("\r\n"):
outf.write(line+"\r")
inf.close()
outf.close()

or, if you're running 2.3:

inf = open("somedosfile", "rU")
outf = open("somemacfile", "wb")
for line in inf:
if line.endswith("\n"):
line = line[:-1]
outf.write(line+"\r")
inf.close()
outf.close()

Something like that should start you in the right direction.

Skip



All times are GMT. The time now is 05:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.