Thread: What's the diff

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    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".
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    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 and Makefiles!
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo