5 Replies - 299 Views - Last Post: 26 June 2013 - 12:50 PM Rate Topic: -----

#1 mouse1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-June 13

Opening and processing text files using python

Posted 25 June 2013 - 12:10 PM

I'm having problem calculating various percentages of text in a text file. Below are my codes. Will appreciate any help, thanks.
     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


This post has been edited by macosxnerd101: 25 June 2013 - 12:19 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0
  • +

Replies To: Opening and processing text files using python

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: Opening and processing text files using python

Posted 25 June 2013 - 12:14 PM

What are you talking about? "The percentages of text in a file" ???
You're multiplying by the filename? WTF? x/100 * "YogiBear.txt" makes no sense.
Was This Post Helpful? 0
  • +
  • -

#3 mouse1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-June 13

Re: Opening and processing text files using python

Posted 25 June 2013 - 12:40 PM

I not trying to multiply by filename, I used the word filename as variable which was I defined in line 4. Please correct the code if you think I'm wrong, if it works, I'll give you credit. Thats the reason I'm here, I not very good.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,755
  • Joined: 27-December 08

Re: Opening and processing text files using python

Posted 25 June 2013 - 01:18 PM

Your code just doesn't make a lot of sense though. First, x is not a number, so it doesn't make sense to divide it by 100. You want to divide the elements in it by 100. Second, you reference it at line 2, but don't declare it until line 5. Your interpreter should be screaming at you with errors.

Third, you never attempt to parse out the data in the File. The Python docs have some good information on File I/O. You should read through them.

Quote

Please correct the code if you think I'm wrong, if it works, I'll give you credit. Thats the reason I'm here, I not very good.

It's one thing to ask for help, but it's another thing to ask us to do your work for you. Even if this isn't homework, we don't support just handing people the answers. This is a pedagogic forum, where we try and guide members in a constructive direction. This is expressed very clearly in the rules when you go to post. Please keep this in mind in the future.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7640
  • View blog
  • Posts: 12,880
  • Joined: 19-March 11

Re: Opening and processing text files using python

Posted 25 June 2013 - 01:33 PM

View Postmouse1, on 25 June 2013 - 02:10 PM, said:

I'm having problem calculating various percentages of text in a text file. Below are my codes. Will appreciate any help, thanks.
     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




This is pretty incoherent. pento is provided when the function is called, but you immediately overwrite it with a new value. Hopefully you have values for x and filename in scope when you call it, or this will fail. Then you return a file object and call it text, and then you try to split that file object, which of course doesn't work. Then you make a list (better to do it this way: x = range(10, 100, 10)) and you take whatever you got from that goofy assignment in the first line of the function and you print it 9 times.

What exactly are you trying to do?

This post has been edited by jon.kiparsky: 25 June 2013 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Opening and processing text files using python

Posted 26 June 2013 - 12:50 PM

A tutorial on reading and writing to/from files. Node that reading (in the way you are using it) always returns a string, and has to be converted to an int or float before calculations can be done. Beginners Guides on the Python.org website = http://wiki.python.o...ide/Programmers

This post has been edited by woooee: 26 June 2013 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1