By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

Opening and processing text in files

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

Expand|Select|Wrap|Line Numbers
  1. def pecent (pento):
  2.     pento = x/100 * filename
  3.     text = open ("C:\\fname.txt", 'r')
  4.     filename = text.split()
  5.     x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
  6.     for z in x:
  7.        print pento
Jun 25 '13 #1
Share this Question
Share on Google+
11 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
In your first line of code:
pento = x/100 * filename
x and filename are undefined.
Jun 26 '13 #2

Rabbit
Expert Mod 10K+
P: 12,441
It also makes no sense to multiply a filename (which I assume is a string) by a number.
Jun 26 '13 #3

P: 11
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?
Jun 27 '13 #4

P: 11
Hi bvdet, x is a number, and filename is a sequence of characters (string). Do I have to explicitly define them?
Jun 27 '13 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Python has specific scoping rules. See Python documentation here. "x" and "filename" must be defined somewhere.
Expand|Select|Wrap|Line Numbers
  1. >>> x = 1000
  2. >>> s = "ABC"
  3. >>> def pecent(): # it makes no sense for pento to be an argument then define it on the next line
  4. ...     pento = x/100*s
  5. ...     print pento
  6. ...     
  7. >>> pecent()
  8. ABCABCABCABCABCABCABCABCABCABC
  9. >>>
Jun 27 '13 #6

P: 11
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.

Expand|Select|Wrap|Line Numbers
  1. x = range (10,100, 10)
  2. f=open ("C:\predator4.txt", 'r')
  3. s = f.read()
  4. def pecent():
  5.     pento = x/100*s
  6.     for z in x:
  7.         print pento
  8. pecent() 
Jun 28 '13 #7

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1.     pento = x/100.0*s     ## 100.0 = float
  2. ## or
  3.     pento = x/float(100)*s  ## float(100) cast to float 
Jun 28 '13 #8

P: 11
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.
Jun 30 '13 #9

bvdet
Expert Mod 2.5K+
P: 2,851
Perhaps you want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. def text_percent(fn, percentage=100.):
  2.     text = open(fn).read()
  3.     pt = percentage/100.
  4.     return text[:int(pt*len(text))]
  5.  
  6. print text_percent("text.txt", 20)
Jul 3 '13 #10

P: 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.
Jul 9 '13 #11

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. for percentage in range(10, 100, 10):
  2.     print text_percent("text.txt", percentage)
Ideally you would open the file once and do all the calculations in a loop.
Expand|Select|Wrap|Line Numbers
  1. def text_percent(fn, *percentages):
  2.     text = open(fn).read()
  3.     return [text[:int(pt/100.*len(text))] for pt in percentages]
  4.  
  5. print "\n\n".join(text_percent("text.txt", 10, 20, 30))
Jul 9 '13 #12

Post your reply

Sign in to post your reply or Sign up for a free account.