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

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0

    Find string in files


    Hi
    Ok so i dont want to append the same text to my file because that would look stupid! Anyway i need some tips on how to search book.txt for the string "Address added" so i dont reappend! So if its already the then goto end of code or? im a n00b

    Thnx everyone
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python has a very elegant way of looking for substrings; using the 'in' keyword. Heres an example:

    Code:
    >>> aString = 'This string can be searched for a substring using the in operator'
    >>> 'This' in aString
    True
    >>> 'searched' in aString
    True
    >>> 'for a substring' in aString
    True
    >>> 'For a substring' in aString
    False
    >>> 'FOR A SUBSTRING' in aString
    False
    >>> 'operator' in aString
    True
    >>> if 'using the in operator' in aString:
    ...     print 'Found the substring!'
    ...
    Found the substring!
    >>>
    Note: This is a case sensitive, if you want to do a case insensitive search then you need to convert the search-string and source-string to the same case; usually to lowercase using the lower() string-method .

    So If you wanted to search a file you would usually read() the file into a variable and then do the search just like I did above: if the substring is found then True is returned, else False.

    Hope this helps ,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0
    kk thnx Another thing that i have not found in any online guids is how to exit the script if something = false.
    Like the good old VB

    Code:
    Dim Age as integer
     age = inputbox("Enter your age:")
       If age > 20 then
    MsgBox "Your way to old! learn python!"
    Unload Me <-Exit he/she is too old!  :eek: 
    Else
    Ect
    lol so i dont want all that to convert in2 python its just an example of how to exit the program!

    Thnx very lots!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    25
    Rep Power
    0
    Put everything into a while loop started and ended by a boolean.

    done = False
    while done = False
    code here...
    to finish the code do this:
    done = True
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0
    K thanX
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could import the sys module and call sys.exit() although by far my favorite way to exit a program is by raising 'SystemExit'.

    Code:
    Netytan@linux:~> python
    Python 2.4 (#1, Dec 10 2004, 19:59:18)
    [GCC 3.3.3 (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> userName = 'Mark'
    >>> if userName == 'Mark':
    ...     raise SystemExit
    ...
    Netytan@linux:~>
    This is amazingly simple, and if you know how to use/handle exceptions in Python then it can be surprisingly powerful .

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    25
    Rep Power
    0
    Nice one =) I haven't gotten that far in the tuturial yet heh. I'm working on sets.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    although by far my favorite way to exit a program is by raising 'SystemExit'.
    Ooh, nice. I've never seen that before.

IMN logo majestic logo threadwatch logo seochat tools logo