473,398 Members | 2,404 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,398 software developers and data experts.

Check for new line character?

I want to skip lines in a file that are blank and that start with "&". So I
strip(None) them and then startswith("&") but the only problem is if the
line has nothing but white space and I strip(None) it then it contains
nothing but a new line character right? So how do I check if the line
contains a new line character? I can no longer use isspace().
Jul 18 '05 #1
3 45073

"Yazar Yolait" <ya***@hotmail.com> wrote in message
news:bn************@ID-198839.news.uni-berlin.de...
I want to skip lines in a file that are blank and that start with "&". So I strip(None) them and then startswith("&") but the only problem is if the
line has nothing but white space and I strip(None) it then it contains
nothing but a new line character right? So how do I check if the line
contains a new line character? I can no longer use isspace().
if line == '\n':
whatever...

If you're using <file>.readline() to
read the lines, then each line (except the eof signal) ***will*** end
with a newline, and it will not have a newline internally, so there's no
need for a complicated check. After you strip blanks, an all blank
line will contain exactly one character: the newline.

John Roth

Jul 18 '05 #2
Yazar Yolait wrote:
I want to skip lines in a file that are blank and that start with "&".
So I
strip(None) them and then startswith("&") but the only problem is if
the
line has nothing but white space and I strip(None) it then it contains
nothing but a new line character right?
No, the newline character is whitespace as well so you'll be left with
the empty string:
' \n'.strip() '' '&\n'.strip()

'&'
So how do I check if the
line
contains a new line character? I can no longer use isspace().


Pretty basic:

while True:
line = inputFile.readline()
if not line:
break
line = line.strip()
if not line or line.startswith('&'):
continue
...

If the rest of your program is sensitive to (non-newline) whitespace,
then just strip the newline, change

line = line.strip()

to

if line[-1] == '\n':
line = line[:-1]

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ To endure what is unendurable is true endurance.
\__/ (a Japanese proverb)
Jul 18 '05 #3
John Roth wrote:
If you're using <file>.readline() to
read the lines, then each line (except the eof signal) ***will*** end
with a newline, and it will not have a newline internally, so there's
no
need for a complicated check.


Not quite true. It's possible the file will be missing the final
newline, though Python will still treat it as a separate line:

max@oxygen:~/tmp% echo newline > test
max@oxygen:~/tmp% echo -n "no newline" >> test
max@oxygen:~/tmp% python
Python 2.3.2 (#1, Oct 3 2003, 15:44:45)
[GCC 3.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
f = file('test')
f.readline() 'newline\n' f.readline() 'no newline' f.readline()

''

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ To endure what is unendurable is true endurance.
\__/ (a Japanese proverb)
Jul 18 '05 #4

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

Similar topics

27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
9
by: MSUTech | last post by:
Hello, What is the best way to check each character within a string? For doing something like encryption, where you check character 1 and replace it with a different character.. then check...
7
by: monomaniac21 | last post by:
Hi guys! Can anyone tell me how you can perform validation on a string variable in PHP so that you can check if a specific character is contained within the string. The reason for this is i am...
4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
3
by: Chakkaradeep | last post by:
Hi all, i have a string, str_old="E:\a\b\c.exe", and i want to change to str_new="E:\\a\\b\\c.exe".
6
by: Stick | last post by:
Hi, I am trying to start my email app (in this case Eudora) with a programatically generated email. However, Eudora does not seem to recognize any of the ways I have tried to create line...
14
by: fniles | last post by:
In VB.NET 2005 can I check if a letter in a string is upper case or lower case ? For example: I have the following 2 lines: NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5...
6
by: GarryJones | last post by:
The following function checks to see if a variable read from a mysql database is numeric. The funtion worked until I hit the value 15 303 That is a valid number but because of the space between...
9
by: stomba | last post by:
Hi, I have a file in this format : 0 3 - - 10 10 1 0 0 0 11 11 I want to read this file and store in a matrix those values:
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
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.