473,938 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting rid of EOL character ?

hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r')
line = Datafile.readli ne()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
Is there a more automatic way to remove the EOL from the string ?

thanks,
Stef Mientki
Apr 27 '07 #1
7 13928
stef wrote:
hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r') line = Datafile.readli ne()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
Is there a more automatic way to remove the EOL from the string ?
line = line.rstrip("\r \n") should take care of it. If you leave out the
parameter, it will strip out all whitespace at the end of the line,
which is what I do in most cases.
--
Michael Hoffman
Apr 27 '07 #2
>
line = line.rstrip("\r \n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.
thanks for the solution Michael,

cheers,
Stef
Apr 27 '07 #3
Jim
If you have a recent Python, see the documentation for open on the
library page for built-in functions.
http://docs.python.org/lib/built-in-funcs.html

Jim

Apr 27 '07 #4
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
stef wrote:
>hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.
Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r') line = Datafile.readli ne()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]
Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.

Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>Is there a more automatic way to remove the EOL from the string ?

line = line.rstrip("\r \n") should take care of it. If you leave out the
parameter, it will strip out all whitespace at the end of the line,
which is what I do in most cases.
If you want *exactly* what is in the line, use line.rstrip('\n ') -- this
will remove only the trailing newline (if it exists).

If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.

Michael, note carefully that line.rstrip('\r \n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].

HTH,
John
Apr 27 '07 #5
hi John,
>>In the previous language I used,
when reading a line by readline, the EOL character was removed.

Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
Yes indeed, and I admit it needs some more coding ;-)
>
>>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r') line = Datafile.readli ne()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
Yes, sorry.
>
>>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?

In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.
Aha, that was the answer I was looking for.

<snip>

thanks for the splendid explanation John,

cheers,
Stef Mientki
Apr 28 '07 #6
John Machin wrote:
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
>stef wrote:
>>hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r') line = Datafile.readli ne()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?

In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.

Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>>Is there a more automatic way to remove the EOL from the string ?

line = line.rstrip("\r \n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.

If you want *exactly* what is in the line, use line.rstrip('\n ') -- this
will remove only the trailing newline (if it exists).

If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.

Michael, note carefully that line.rstrip('\r \n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].
I knew that about line.rstrip, but didn't consider the possibility of
\r\r\n, while still wanting the first \r. Yuck.

Honestly, I almost always use line.rstrip()--it is seldom that I care
about closing whitespace.
--
Michael Hoffman
Apr 28 '07 #7
On Apr 28, 7:25 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
John Machin wrote:
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
stef wrote:
hello,
>In the previous language I used,
when reading a line by readline, the EOL character was removed.
Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,' r') line = Datafile.readli ne()
>now this gives an extra empty line
print line
>and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]
Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>while this gives what I need ???
print line[,-1]
>Is it correct that the 2 characters CR+LF are converted to 1 character ?
In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.
Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>Is there a more automatic way to remove the EOL from the string ?
line = line.rstrip("\r \n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.
If you want *exactly* what is in the line, use line.rstrip('\n ') -- this
will remove only the trailing newline (if it exists).
If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.
Michael, note carefully that line.rstrip('\r \n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].

I knew that about line.rstrip, but didn't consider the possibility of
\r\r\n, while still wanting the first \r. Yuck.
It would be unusual to want that first \r -- a possibly more likely
scenario might be where your text file contains an extract from a
database, and you need to check that there are no unwanted (e.g.
unprintable) characters in the data (whether at the end of the line,
the middle, or the start).

In any case I think that you are missing the point that when reading a
normal text file on Windows with readline, while the line in the file
may be 'foo bar\r\n', what you get from readline is 'foo bar\n' -- so
in normal usage, the \r in your line.rstrip('\r \n') is pointless.
>
Honestly, I almost always use line.rstrip()--it is seldom that I care
about closing whitespace.
Honestly, I almost always split a line into fields and then for each
field, strip leading and trailing whitespace, and change runs of 1 or
more whitespace characters to a single space -- where "whitespace "
includes the pesky U+00A0 aka &nbsp; which doesn't qualify as
whitespace in a str instance.

Cheers,
John

Apr 28 '07 #8

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

Similar topics

5
2551
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already started sending out the content of the page. Web content is always delivered with a few headers at the top, ending with a blank line. For example, a web page might start like this:
4
10975
by: Surya Kiran | last post by:
Hi all, I'm facing a wierd problem. I've a file, which is getting updated every now and then. and i'm having another program, which monitors the file. I've to read the file line by line, and in the end, i've to find out whether the line has end of line character ('\n') in it or not. if its not there (that means the line is only partially written), i've to discard it. I'm using std::string::getline() function, with the delimiter as '\n'....
5
3293
by: Mike Murray | last post by:
Hi I have the following issue. I have a character that is return by a SQL Server database "É" to be precise, the issue is that when I store character in a .net string variable my understanding is that it converts it to UTF-16 automatically. I've seen a lot of quotes saying "All String datatype variables in .NET are UTF-16 encoded". The thing is that I want to write this character to a XML file in UTF-8 encoded format instead. How do I...
17
14396
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart, int longueur) { char *resultat = " "; char *temporaire = " "; int nbr;
5
2304
by: Durgesh Sharma | last post by:
Are there no genric Macros in c to represent Integers,Characters,...or other data types ? I want to pass that MACRO (representing an alpha numeric Character)to that strrchar() function,to get the last occurence of the last NON SPACE or Alphanumeric character,then i want to increase the position by one and add a NULL char ....This is my IDEA of rtrim function.....pls help me out..... How can i get a way to represent genric alphanumeric...
9
2294
by: Velvet | last post by:
I'm trying to convert some JavaScript to C# and don't know how to get the character code of a character in a string. in JavaScript it is as follows: for( i = 0; i < email.length; i++) { var fs = email.charCodeAt(i); uniemail = uniemail + '&#' + fs + ';';
36
3236
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with the needed data. Any ideas? Here's the code in question, see any reason why a command would not trigger the 'kbhit' the first time a serial command is sent?: Thanks! Chuck **************************************************** while(1) //...
1
5567
by: prakash.appidi | last post by:
Hi I am getting the following error when I tried to run the code given below ORA-06550: line 1, column 29: PLS-00553: character set name is not recognized ORA-06550: line 0, column 0: PL/SQL: Compilation unit analysis terminated can anyone explain me why am getting such error and how to resolve it...
3
2567
by: Jeff | last post by:
I have had this function work perfectly in IE and am trying to get it to work in Firefox. I have seen plenty of questions and answers on the web for how to get and check the key pressed but nothing about setting or changing it. I only want uppercase characters in this case. If the key is a lowercase, it needs to be changed to an uppercase character. IE lets you use keyCode to change the character code. How do I do the equivalent in...
1
16000
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h and psapi.lib are in this same folder as process.c file, i am getting lonf list of errors from psapi.lib Process.c (from http://voice.mytechnopark.com/viewtopic.php?id=51) #include <windows.h> #include <stdio.h>
0
10132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
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,...
0
10653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9854
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
8214
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
7378
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4901
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
4444
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.