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
+ 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 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 Jun 25 '13 #1
Share this Question
11 Replies

 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

 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

 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 >>> 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 >>> 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 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()  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     pento = x/100.0*s     ## 100.0 = float ## or     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

 Expert Mod 2.5K+ P: 2,851 Perhaps you want to do something like this: Expand|Select|Wrap|Line Numbers 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) 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

 Expert Mod 2.5K+ P: 2,851 Expand|Select|Wrap|Line Numbers 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. Expand|Select|Wrap|Line Numbers 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)) Jul 9 '13 #12

### Post your reply

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