Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 16th, 2012, 03:40 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
What's the diff

i have a text file resembling something like:

asfasdf
fkkfs
aghtj
sjsga
mwyha
kstjrjtjks
ehashah
adfgaa
eraaf

i read it in like this:

Code:
Lines = open('filename', 'r').readlines()
  for Line in Lines :
    if Line == 'mwyha' : print Line

i get no printed Line. however if i code it like this:

Code:
Lines = open('filename', 'r').readlines()
  for Line in Lines :
    if 'mwyha' in Line : print Line


i get Line showing up printed out...i don't understand why the first way doesn't work...any suggestions?

Reply With Quote
  #2  
Old July 16th, 2012, 03:53 PM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
The difference between "str1 == str2" and "str1 in str2" is that the former returns true if both strings are equal and the latter returns true if str1 is a substring of str2. So if str2 is "FOOhelloBAR" and str1 is "hello", then "str1 == str2" will obviously return false while "str1 in str2" will return true.

In your case the reason that the two strings are not equal is that readlines() does not strip off the newline character from the lines it reads, so Line will be equal to "mwyha\n" - not "mwyha".

Reply With Quote
  #3  
Old July 16th, 2012, 03:56 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
readlines() retains the end of line characters, on my system a linefeed. These are apparent if you have statements like:

print(len(Line),Line)
print(ord(Line[-1]))
Comments on this post
WynnDeezl agrees: Thank you!
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old July 16th, 2012, 05:07 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
Instead of using readlines, you can always now read from the file object directly.
Code:
file = open('filename', 'r')
for line in file:
    line = line.strip()
    if line == 'mywha': print line
file.close()


or if you want to compact it a bit:
Code:
for line in open('filename', 'r'):
    line = line.strip()
    if line == 'mywha': print line

In this case, the file will be automatically closed and garbage collected when it falls out of scope of the for loop.

Either of these two approaches is more memory-efficient than using readlines() because they don't read the entire file into memory ahead of time, just enough chunks to get the job done.
Comments on this post
WynnDeezl agrees: Thank you!
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > What's the diff

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap