473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

\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 10410
aj****@gmail.co m 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.co m <aj****@gmail.c om> 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.co m <aj****@gmail.c om> 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.t xt')
for line in my_file:
# whatever...

--
Cheers,
Simon B,
si***@brunningo nline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #5
aj****@gmail.co m 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.c om> 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.c om> 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

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

Similar topics

5
28048
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. How do I fix Notepad?
6
7486
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 specification: "Quote starts": 5.6 Child selectors
3
4857
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 a process of notepad on the server , but does not open the file in notepad. What am I doing wrong? can you show me how to do it please? What I am doing here is : I have datagrid data and when clicked on linkbutton (logFile), it finds the row...
2
11163
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 want to change its attributes to Readonly I do not want to open it in browser or in any other editor except notepad I do not want to use MFCAppWizard of VC++ or VB to create my own editor. I do not want to paste the text in some richtextbox...
2
3253
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. When I edit the web pages in notepad I see a square wherever I normally see a carriage return and a line feed when viewing the source in I.E. How can I make the web pages come down readable so I can easily make my two manual deletions...
10
1941
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 full-fledge IDE such as VS.NET 2005 which gives code-insight, debugger, etc? Regards, James.
6
2465
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 editor and the compiler were just one unique thing :-) Can anybody help me? thanks
3
1652
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 party application that allows users to create customizable files within certain syntax rules. There isn't, however, a good editor for these files (users end up using Notepad, I guess) and I would like to create one...most likely using Visual Studio....
36
5397
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 are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather than retrieve the text into my app or force the user to use another app to read the file. I'm...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.