LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2011, 04:51 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Basis Python scripting


Hi all

I'm trying to find my way in Python scripting.
I got some code from a LQ user, and i want to build on it.

I got this:
Code:
fh = open("file.txt")
for line in fh:
    if 'test' in line:
        print (line)
fh.close()
So it opens a file and searches for a word 'test', and when he finds it, he prints it, and this is working fine, BUT i get enters where i don't need them, this is my output

Code:
C:\Users\..\Dekstop>python test.py
test

test123

C:\Users\..\Desktop>
So i want to filter those enters out, but i don't know how
Does anyone know how i can filter those '123' away?

Thanks in advance and i'm trying to find it myself until someone can guide me

 
Old 09-06-2011, 06:27 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
The problem is that the line has a return ('\r\n') on the end of it. You should be able to get it to work if you change 'print (line)' to
Code:
print line.rstrip("\r\n")
For your second question, you'll have to expand a bit - do you mean you just want to print 'test'? Because... if you're searching for test... then you know what 'test' is. If you see what I mean. Perhaps give us some more examples, and tell us which bits of the line you would want printed
 
Old 09-06-2011, 06:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try striping off the new lines:
Code:
print (line.rstrip())
 
Old 09-06-2011, 09:41 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416

Original Poster
Blog Entries: 2

Rep: Reputation: 12
oke, thanks fella's
I'll try that tomorrow!
 
Old 09-06-2011, 09:57 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by Snark1994 View Post
The problem is that the line has a return ('\r\n') on the end of it. You should be able to get it to work if you change 'print (line)' to
Code:
print line.rstrip("\r\n")
For your second question, you'll have to expand a bit - do you mean you just want to print 'test'? Because... if you're searching for test... then you know what 'test' is. If you see what I mean. Perhaps give us some more examples, and tell us which bits of the line you would want printed
There's no need to specify "\r\n". Python (rstrip) knows how to take care of newlines
 
Old 09-07-2011, 10:32 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by kurumi View Post
There's no need to specify "\r\n". Python (rstrip) knows how to take care of newlines
Thanks, I remembered once grail posted his solution
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python scripting sammymcgee Programming 4 03-23-2011 01:37 PM
LXer: Android Scripting With Python LXer Syndicated Linux News 0 08-20-2010 12:51 AM
PYTHON or Bash Shell Scripting? latinmusic74 Linux - Newbie 4 06-04-2010 10:58 AM
Shell Scripting/Python/C MCD_Thom Linux - Newbie 4 11-09-2004 11:40 AM
Python and shell scripting nixdisciple Programming 3 06-07-2002 07:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration