Opening and processing text in files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tiger1
    New Member
    • Jun 2013
    • 11

    Opening and processing text in files

    I'm having problem calculating various percentages of text in a text file. Below are my codes. Will appreciate any help, thanks.

    Code:
    def pecent (pento):
        pento = x/100 * filename
        text = open ("C:\\fname.txt", 'r')
        filename = text.split()
        x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
        for z in x:
           print pento
    Last edited by Rabbit; Jun 25 '13, 07:57 PM. Reason: Please use code tags when posting code.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    In your first line of code:
    pento = x/100 * filename
    x and filename are undefined.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      It also makes no sense to multiply a filename (which I assume is a string) by a number.

      Comment

      • Tiger1
        New Member
        • Jun 2013
        • 11

        #4
        Hi Rabbit, you are right, I'm trying to multiply a string by a number. I came across something that has to do with converting to a float first b4 multiplying, but i'm trying to figure out how to do this conversion. Any ideas?

        Comment

        • Tiger1
          New Member
          • Jun 2013
          • 11

          #5
          Hi bvdet, x is a number, and filename is a sequence of characters (string). Do I have to explicitly define them?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Python has specific scoping rules. See Python documentation here. "x" and "filename" must be defined somewhere.
            Code:
            >>> x = 1000
            >>> s = "ABC"
            >>> def pecent(): # it makes no sense for pento to be an argument then define it on the next line
            ... 	pento = x/100*s
            ... 	print pento
            ... 	
            >>> pecent()
            ABCABCABCABCABCABCABCABCABCABC
            >>>

            Comment

            • Tiger1
              New Member
              • Jun 2013
              • 11

              #7
              Hi bvdet, thanks for your response. You've really been of help to me. Your code worked, but only when x is 100 and above. It does not slice through the text (can't produce 90%, 80,--10% of the text). Besides I would like to loop through in order to get the above proportions of the text. I keep getting the following error: TypeError: unsupported operand type(s) for /: 'list' and 'int'. Below are my lines of code.

              Code:
              x = range (10,100, 10)
              f=open ("C:\predator4.txt", 'r')
              s = f.read()
              def pecent():
                  pento = x/100*s
                  for z in x:
                      print pento
              pecent()

              Comment

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                If you are running Python 2.X and not 3.X then divide returns an int. You have to tell it to use floats by providing at least one number that is a float.
                Code:
                    pento = x/100.0*s     ## 100.0 = float
                ## or
                    pento = x/float(100)*s  ## float(100) cast to float

                Comment

                • Tiger1
                  New Member
                  • Jun 2013
                  • 11

                  #9
                  Hi dwblas, sorry i couldn't communicate the outcome of the above solution in time. I tried using float as you suggested but I was still getting the same error message. Can you just try the code on text document to see if it will work? if it works, just let me know the version of python you are using. Thanks in advance.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Perhaps you want to do something like this:
                    Code:
                    def text_percent(fn, percentage=100.):
                        text = open(fn).read()
                        pt = percentage/100.
                        return text[:int(pt*len(text))]
                    
                    print text_percent("text.txt", 20)

                    Comment

                    • Tiger1
                      New Member
                      • Jun 2013
                      • 11

                      #11
                      Hi bvdet, thanks for the reply. Your solution works fine. I have one more question for you: how do I create a loop that will enable me compute 10, 20,.....,90% of the text in the document at once, instead of computing a single percentage each time?

                      Thanks pal.

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Code:
                        for percentage in range(10, 100, 10):
                            print text_percent("text.txt", percentage)
                        Ideally you would open the file once and do all the calculations in a loop.
                        Code:
                        def text_percent(fn, *percentages):
                            text = open(fn).read()
                            return [text[:int(pt/100.*len(text))] for pt in percentages]
                        
                        print "\n\n".join(text_percent("text.txt", 10, 20, 30))

                        Comment

                        Working...