473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

\r\n or \n notepad editor end line ???

Hello,

I use windows notepad editor to write text.

For example I write (in d:\myfile.txt):
Helo
World

If I open it with python:
FName = open(d:\myfile.txt,'r')
h = FName.readlines()
print h

I get h : ['Helo\n', 'World']

I thought notepad use \r\n to to end the line.

What's wrong with it?

pujo

Jul 19 '05 #1
15 10370
aj****@gmail.com wrote:
Hello,

I use windows notepad editor to write text.

For example I write (in d:\myfile.txt):
Helo
World

If I open it with python:
FName = open(d:\myfile.txt,'r')
h = FName.readlines()
print h

I get h : ['Helo\n', 'World']

I thought notepad use \r\n to to end the line.

What's wrong with it?

Python tries to be clever. Open it in binary mode to avoid it:

FName = open(d:\myfile.txt,'rb')
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 19 '05 #2
On 8 Jun 2005 06:24:05 -0700, aj****@gmail.com <aj****@gmail.com> wrote:
Hello,

I use windows notepad editor to write text.

For example I write (in d:\myfile.txt):
Helo
World

If I open it with python:
FName = open(d:\myfile.txt,'r')
h = FName.readlines()
print h

I get h : ['Helo\n', 'World']

I thought notepad use \r\n to to end the line.

What's wrong with it?


On windows, opening a file without 'b' in the file type argument does
some things you might not expect, including changing /r/n to /n. Try:
f = file('d:/deleteme.txt', 'rb')
f.read() 'testing\r\n1\r\n2\r\n3' f = file('d:/deleteme.txt', 'r')
f.read()

'testing\n1\n2\n3'

Peace
Bill Mill
bill.mill at gmail.com
Jul 19 '05 #3
Hello thanks everyone,

It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?

pujo

Jul 19 '05 #4
On 8 Jun 2005 06:44:40 -0700, aj****@gmail.com <aj****@gmail.com> wrote:
It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?


It depends what you are trying to do with the file. If you are
processing it as a text file, open it as a text file, and all will be
well:

my_file = open('my_file.txt')
for line in my_file:
# whatever...

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #5
aj****@gmail.com wrote:
It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?


There is a conceptual difference between "text" files and other files
(which are lumped under the label "binary").

Binary files have any kind of data in them (bytes from 0 to 255) and no
inherent concept of "lines", and thus no line ending sequences.

Text files generally have no control characters except those used to
terminate the lines (lines are by definition sequences of bytes followed
by the line ending sequence). The line ending sequence is
platform-dependent: Linux and most other things use just \n (LineFeed),
while Windows/DOS uses \r\n (CarriageReturn + LineFeed) and the old
MacOS used just \r (CarriageReturn).

Since the specific line ending used on your platform is rarely important
to you, so long as you are compatible with other applications that use
"text" files (such as Notepad), you should use just "r" and "w" to read
and write files that you consider "text" (and note: it's just a
convention, in your mind... and at some times a given file might be
treated the other way, quite legitimately). Python (actually the
underlying libraries, I believe) will convert the platform-specific line
endings to \n when reading text files, and will convert \n to the proper
line ending sequence for your platform when writing.

If you don't want this conversion, which is unlikely if this is really
just a text file, then and only then do you want to use "rb" and "wb".

So the answer to the question "What should I be using?" depends entirely
on you: if you were interested in seeing the raw bytes that Notepad
wrote, then use "rb". If you want to work with that file as a *text*
file, then use just "r".

Note also the existence of the "U" modifier. Opening a file with "rU"
will read any of the aforementioned line-ending sequences and convert
them to just \n, allowing you to work with text files created on other
platforms. (I don't believe there's a "wU" and conceptually it's sort
of meaningless anyway, so you would just use "w" to write the file out
again.)

-Peter
Jul 19 '05 #6
Hello All,

Thanks for the response.

I use mysql and find something strange lately while load text file to
my database table using LINES TERMINATED BY '\r\n',

And I found that mysql think I have '\r\r\n'. this is happened because
in one of my code I use 'w' to write element of string + '\r\n'. now I
understand why this happened.

Actually I prefer to use 'w' and 'r' because the termination of the
line will always '\n'.
By changing mycode to always use 'w' and write element of string +'\n'
instead of +'\r\n' and let my mysql code use LINES TERMINATED '\r\n' I
think solve this problem. :)

pujo

Jul 19 '05 #7
Peter Hansen wrote:
(I don't believe there's a "wU" and conceptually it's sort
of meaningless anyway,


If we ever get quantum computers, presumably "wU" will
write the newlines in all possible formats simultaneously...

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #8
<aj****@gmail.com> wrote:
It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?


no.

you should use "wb" to write *binary* files, and "rb" to read *binary*
files.

if you're working with *text* files (that is, files that contain lines of text
separated by line separators), you should use "w" and "r" instead, and
treat a single "\n" as the line separator.

</F>

Jul 19 '05 #9
On Mon, 13 Jun 2005 11:53:25 +0200, Fredrik Lundh wrote:
<aj****@gmail.com> wrote:
It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?


no.

you should use "wb" to write *binary* files, and "rb" to read *binary*
files.

if you're working with *text* files (that is, files that contain lines of text
separated by line separators), you should use "w" and "r" instead, and
treat a single "\n" as the line separator.


I get nervous when I read instructions like this. It sounds too much like
voodoo: "Do this, because it works, never mind how or under what
circumstances, just obey or the Things From The Dungeon Dimensions will
suck out your brain!!!"

Sorry Fredrik :-)

When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline? Do you have to handle it yourself? Or will
Python cleverly suppress it so you don't have to worry about it?

And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name? Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.

How does this behaviour differ from "universal newlines"?

--
Steven
Jul 19 '05 #10
Steven D'Aprano wrote:
When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline? Do you have to handle it yourself? Or will
Python cleverly suppress it so you don't have to worry about it?

And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name? Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.
This is Python. Fire up the interactive interpreter and try it out! It
will take all of two or three minutes...
How does this behaviour differ from "universal newlines"?


If you open a text file created with a different line-ending convention
than that used on your own platform, you may get "interesting" results.
If you use "rU" instead, you will receive only \n line endings and not
have anything to worry about. (For example, reading a Windows text file
on Linux will give you lines that have \r\n endings in them... not what
you really want. Using "rU" will give you just \n line endings whether
you are on Linux or Windows.)

-Peter
Jul 19 '05 #11
On Monday 13 June 2005 09:34 am, Steven D'Aprano wrote:
On Mon, 13 Jun 2005 11:53:25 +0200, Fredrik Lundh wrote:
<aj****@gmail.com> wrote:
It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right? no.
you should use "wb" to write *binary* files, and "rb" to read *binary*
files.

if you're working with *text* files (that is, files that contain lines of text
separated by line separators), you should use "w" and "r" instead, and
treat a single "\n" as the line separator.


I get nervous when I read instructions like this. It sounds too much like
voodoo: "Do this, because it works, never mind how or under what
circumstances, just obey or the Things From The Dungeon Dimensions will
suck out your brain!!!"


Actually, it's very simple. It just means that '\n' in memory maps to '\r\n' on
disk, and vice-versa. So long as you remain on Windows (or MS-DOS for
that matter).
When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline? Do you have to handle it yourself? Or will
Python cleverly suppress it so you don't have to worry about it?
'\n' in memory, '\r\n' on disk.

Do the same thing on a Linux or Unix system, and it's

'\n' in memory, '\n' on disk

and on the Mac:

'\n' in memory, '\r' on disk
And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name? Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.


No, all will be well. So long as you use 'r' (or 'rt' to be explicit) and 'w'
(or 'wt').

Only use 'rb' and 'wb' if you want to make sure that what is on disk is
literally the same as what is in memory. If you write text files this way
in Python, you will get '\n' line endings.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #12
On Mon, 13 Jun 2005 10:52:52 -0400, Peter Hansen wrote:
Steven D'Aprano wrote:
When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline? Do you have to handle it yourself? Or will
Python cleverly suppress it so you don't have to worry about it?

And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name? Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.
This is Python. Fire up the interactive interpreter and try it out! It
will take all of two or three minutes...


Which I would have done, except see comment below:
How does this behaviour differ from "universal newlines"?


If you open a text file created with a different line-ending convention
than that used on your own platform, you may get "interesting" results.


I'm using Linux, not Windows. Of course I *could* try to fake reading and
writing Windows files from Linux, but that would require me actually
understanding how Python deals with line endings across the platforms in
order to generate them in the first place. But if I understood it, I
wouldn't have needed to ask the question.

And I would still be none the wiser about Python's behaviour when running
under Windows -- that's hard to fake on a Linux box.
If you use "rU" instead, you will receive only \n line endings and not
have anything to worry about. (For example, reading a Windows text file
on Linux will give you lines that have \r\n endings in them... not what
you really want. Using "rU" will give you just \n line endings whether
you are on Linux or Windows.)


So going back to the original question... if I open in "r" mode a text
file which was created under Windows, I will get \r characters in the
text and have to deal with them regardless of what platform I am running
Python under. Correct?

If I use "rU" mode Python suppress the \r characters. When I write a
string back, I'm responsible for making sure the EOL markers are correct
for whatever platform I expect the file to be read under. Unless Python
can somehow magically read my mind and know that even though I'm writing
the file under Linux it will be read later under Windows. Am I close?
(Ew, I hate cross-platform issues. They make my head hurt.)
--
Steve.

Jul 19 '05 #13
Steven D'Aprano wrote:
On Mon, 13 Jun 2005 11:53:25 +0200, Fredrik Lundh wrote:

<aj****@gmail.com> wrote:

It means in windows we should use 'wb' to write and 'rb' to read ?
Am I right?
no.

you should use "wb" to write *binary* files, and "rb" to read *binary*
files.

if you're working with *text* files (that is, files that contain lines of text
separated by line separators), you should use "w" and "r" instead, and
treat a single "\n" as the line separator.

I get nervous when I read instructions like this. It sounds too much like
voodoo: "Do this, because it works, never mind how or under what
circumstances, just obey or the Things From The Dungeon Dimensions will
suck out your brain!!!"

Sorry Fredrik :-)


Many people don't appear to want to know why; they only want a solution
to what they perceive to be their current problem.
When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline?
The thing to which you refer is not a "newline". It is an ASCII LF
character. The CR and the LF together are the physical representation
(in a Windows text file) of the logical "newline" concept.

Internally, LF is used (irrespective of platform) to represent that concept.
Do you have to handle it yourself?
No.
Or will
Python cleverly suppress it so you don't have to worry about it?
Suppressed: no, it's a transformation from a physical line termination
representation to a logical one. Cleverly: matter of opinion. By Python:
In general, no -- the transformation is handled by the underlying C
run-time library.

And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name?
If they do, it will not be because other than CRLF has been written as a
line terminator.
Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.
AFAIR, it performs well enough for a text editor presented with a file
consisting of one long unterminated line with occasional embedded
meaningless-to-the-editor control characters. You can scroll it, edit
it, write it out again ... any crankiness is likely to be between the
keyboard and the chair :-)

How does this behaviour differ from "universal newlines"?


Ordinary behaviour in text mode:

Win: \r\n -> newline -> \r\n
Mac OS X < 10: \r -> newline -> \r
other box: \n -> newline -> \n

Note : TFM does appear a little light on in this area. I suppose not all
users of Python have aquired this knowledge by osmosis through decades
of usage of C on multiple platforms :-)

"Universal newlines":
On *any* box: \r\n or \n or \r (even a mixture) -> \n on reading
On writing, behaviour is "ordinary" i.e. the line terminator is what is
expected by the current platform

"Universal newlines" (if used) solves problems like where an other-boxer
FTPs a Windows text file in binary mode and then posts laments about all
those ^M things on the vi screen and :1,$s/^M//g doesn't work :-)

HTH,
John
Jul 19 '05 #14
Steven D'Aprano wrote:
So going back to the original question... if I open in "r" mode a text
file which was created under Windows, I will get \r characters in the
text and have to deal with them regardless of what platform I am running
Python under. Correct?
Almost, but the way you phrased that makes it incorrect in at least one
case. I suspect you are clear on the actual situation now, but just to
correct your wording: you will get \r characters and have to deal with
them on any platform which does *not* use \r\n for its line endings.
(Under Windows, of course, you won't have to deal with them at all.)
If I use "rU" mode Python suppress the \r characters. When I write a
string back, I'm responsible for making sure the EOL markers are correct
for whatever platform I expect the file to be read under. Unless Python
can somehow magically read my mind and know that even though I'm writing
the file under Linux it will be read later under Windows. Am I close?


Python can't magically read your mind, although if you manage to steal
the time machine (in the future) you could always just come back and fix
the files as they are written, and you (in present time) would *think*
that Python had magically read your mind...

A few more trinkets: some programs on Windows will work just fine even
if they encounter files with only \n line endings. Decent text editors
will either adapt or let you specify what line ending should be
expected. Trying always to work with only \n line endings in files you
write will help us move towards the day when we can obliterate the
abomination that is \r\n so try it out and use "rU" to read, "w" to
write on Linux if that works in your case.

-Peter
Jul 19 '05 #15
John Machin wrote:
Many people don't appear to want to know why; they only want a solution
to what they perceive to be their current problem.


and many people can identify a short HOWTO when they see it, and look
things up in the documentation when they want the full story. reposting
the documentation in response to every question is, in most cases, a
waste of time and bandwidth...

</F>

Jul 19 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: aa | last post by:
I write to a text file, and when view the resulting file in Notepad, it shows "\t" as tabs correctly, but "\n" does not break the line. Instad it shows as a square. In Dreamweaver it shows OK....
6
by: Patrick | last post by:
Hi I am a newbie struggling a little with css.It is hard to get it right in all browsers, so i decided to read the CSS2 specification on the w3 site. What is the following from the CSS2...
3
by: RN Das | last post by:
Hi expert, I have a small web application in C# and ASP.Net and I want to open a file in notepad when clicked on a linked button. Here is hander code when clicked on the link button. It can open...
2
by: Suma | last post by:
Hi To open a notepad file, right now I am using Process.start. But this does not ensure that the file is opened in readonly mode How can I make a notepad file open in Readonly mode I do not...
2
by: Lila Godel | last post by:
I am having a problem with the download of web pages via the WebClient.DownloadFile function in the specialized VB.Net 2003 I.E. plug-in I am designing to speed up the work on my latest project. ...
10
by: arlef | last post by:
Hi Guys, When learning a language such as C#, would you say it is better to learn the fundamentals using the command-line compiler and a simple text editor such as notepad compared to using a...
6
by: marco.delvecchio | last post by:
I downloaded the compiler C++ 5.5 from borland site... where can i find an easy to use editor that can be integrated with this compiler?... the last time i was programming in C was in 1993.. the...
3
by: GreggTB | last post by:
I've looked online for a while but haven't found a whole lot of really good resources for this, so I'm hoping that maybe someone here can point me in the right direction. I'm working with a 3rd...
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.