searching within a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LolaT
    New Member
    • Jul 2007
    • 22

    searching within a file

    Does anyone know where I can read up on how to search for a particular object in a file? For example, search for a particular word in a dictionary?
  • linksterman
    New Member
    • Jul 2007
    • 38

    #2
    Code:
    file("file name.txt", 'rb')
    fil.find("word")

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Not much to it really:[code=Python]>>> f = open(r'C:\SDS2_ 7.0\jobs\60801_ Baylor\macro\De epEllumRad1.txt ')
      >>> for line in f:
      ... if 'Deep Ellum' in line:
      ... print line
      ...
      # Deep Ellum radii and cons lines at Canopy 1 (B-D)

      >>> f.close()
      >>> [/code]Do you have a specific problem you are trying to solve?

      Comment

      • LolaT
        New Member
        • Jul 2007
        • 22

        #4
        Originally posted by bvdet
        Not much to it really:[code=Python]>>> f = open(r'C:\SDS2_ 7.0\jobs\60801_ Baylor\macro\De epEllumRad1.txt ')
        >>> for line in f:
        ... if 'Deep Ellum' in line:
        ... print line
        ...
        # Deep Ellum radii and cons lines at Canopy 1 (B-D)

        >>> f.close()
        >>> [/code]Do you have a specific problem you are trying to solve?

        Thanks for the advice.
        I'm trying to create a spell checker which can spell check either a single word or a file depending on what the user chooses. The spell checker is really simple though, it doesn't point out a word that is spelled wrong, however it checks two different files to see if the word(s) is/are typed in are contained in either file. One of the files is a dictionary file with a list of words, and the other file is the user dictionary, created by the user.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by LolaT
          Thanks for the advice.
          I'm trying to create a spell checker which can spell check either a single word or a file depending on what the user chooses. The spell checker is really simple though, it doesn't point out a word that is spelled wrong, however it checks two different files to see if the word(s) is/are typed in are contained in either file. One of the files is a dictionary file with a list of words, and the other file is the user dictionary, created by the user.
          You are welcome. Let us know how the project goes for you.

          Comment

          • anonymous
            Banned
            New Member
            • Sep 2005
            • 99

            #6
            Code:
            import re
            authorpattern = "<meta name="AUTHOR" content="(.*?)"><meta name="
            patt = re.compile(authorpattern,re.IGNORECASE)
            
            authors = re.findall(patt,data)
             
            or if you need only 1 author
            
            amatch = re.search(source.authorpattern,data,re.IGNORECASE)
            author = amatch.group(1)
            Last edited by bartonc; Jul 27 '07, 07:23 PM. Reason: Added [CODE][/CODE] tags.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by anonymous
              Code:
              import re
              authorpattern = "<meta name="AUTHOR" content="(.*?)"><meta name="
              patt = re.compile(authorpattern,re.IGNORECASE)
              
              authors = re.findall(patt,data)
               
              or if you need only 1 author
              
              amatch = re.search(source.authorpattern,data,re.IGNORECASE)
              author = amatch.group(1)
              Hello, annonymous! I'm impressed: 49 posts; C++, Java, MySQL, now Python and a regex to boot.

              I welcome your contributions here in the Python forum. I must, however, that you use
              Code:
               tags any time that you post executable lines. And [CODE=python]# Adding =python to them makes them look even better.
              Thank you.

              Comment

              • LolaT
                New Member
                • Jul 2007
                • 22

                #8
                thanks again to everyone for their help.
                i'm encountering a major problem with my project though.
                i'm supposed to take the file named dict.txt which has quite the amount of words
                and turn it into a list.
                i know how to do it (at least i think so) but my program keeps shutting down, i'm guessing because the file is pretty large.
                any suggestions?

                Comment

                Working...