We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Need to pull numbers from a file and get the sum

megaapps
megaapps asked
on
Medium Priority
218 Views
Last Modified: 2012-05-11
Hi all I need help pulling integers from a file in python. I have a file that is in csv format and has 200000 numbers in it. And I need to pull all of the numbers out and insert them into a list and then get the mean of the numbers that are in the list. I have tried but the problem is it won't let me convert the list into integers. Attached is some code that I have so far.
def loadFile(nameOfFile):
    if(nameOfFile == 'Alpha'):
        fileopen =  open('Alpha.txt', 'rU')
        csvReader = csv.reader(fileopen,'excel')
        insert = list()
        for row in csvReader:
         insert.append(row)
        fileopen.close()
    return insert



def menu():    
    
  loopFlag = True
  while loopFlag:
    print "Homework #7 Menu"
    print "1. Load File"
    print "2. Print the Mean of all the numbers"
    print "3. Print the Standard Deviation of all the numbers"
    print "4. Generate a table based on Mean and Standard Deviation"
    print "q. Quit"
    print
    choice=raw_input(" Please enter your selection")
    
    if (choice == '1'):
       nameOfFile = raw_input("Please enter name of file you want to open")
       l =  loadFile(nameOfFile) 
       
       
    elif (choice == '2'):
       print sum(l)
    elif (choice == '3'):
       pass
    elif (choice == '4'):
       pass
    elif (choice =='q') or (choice =='Q'):
        loopFlag=False
    else:
        print "Invalid Menu Item"
  else:
    raw_input("Done")
    return

Open in new window

Comment
Watch Question

Commented:
It sounds like you are loading the file and parsing the sections of string out just fine... so you need to convert each hunk of string into an integer you will need this function:

int([x[, base]])

Convert a string or number to a plain integer. If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace. The base parameter gives the base for the conversion (which is 10 by default) and may be any integer in the range [2, 36], or zero. If base is zero, the proper radix is determined based on the contents of string; the interpretation is the same as for integer literals. (See Numeric literals.) If base is specified and x is not a string, TypeError is raised. Otherwise, the argument may be a plain or long integer or a floating point number. Conversion of floating point numbers to integers truncates (towards zero). If the argument is outside the integer range a long object will be returned instead. If no arguments are given, returns 0.

The integer type is described in Numeric Types — int, float, long, complex.

Open in new window


This is a built in function of the language, so should show itself to you.

Good luck.
CERTIFIED EXPERT

Commented:
As it is a homework, you cannot expect the ready-to-be-used solution.  But you can expect some help assuming you will do it oneself.  Some notes related to your loadFile() function...

def loadFile(nameOfFile):
    if(nameOfFile == 'Alpha'):
        fileopen =  open('Alpha.txt', 'rU')
        csvReader = csv.reader(fileopen,'excel')
        insert = list()
        for row in csvReader:
         insert.append(row)
        fileopen.close()
    return insert

Open in new window


Apparently, the code does not compile as you at least have to import the csv module.

First of all, you should choose better identifiers.  For example, the "insert" is not a good identifier for ANY object.  Let the poorly chosen "fileopen" be just "f".  The csv.reader() is usually given the name "reader" as you want to emphasize that you get some info through it, and you do not want to emphasize what is hidden behind it.  Never use "l" identifier (i.e. lower-case L) as it can be easily confused with "1" when some fonts are used -- "lst" is also fine here (in the body of the script, outside the loadFile()).

The empty list could be created via list(), but it is more usual to use the [].  Let the auxiliary list be called simply "lst".

Keep the style guide recommendation -- indent each level by 4 spaces.

Put "print repr(row)" before or after your .append(row) to see what is pushed into the list.

The parentheses around the condition used in "if" are neccessary in some languages, not in Python.  Do not make it looking the same as in other languages.  Python is a bit different.  Using the extra parentheses will not make it more readable.

Some purists may say that "while True: ... break" is unacceptable.  But the intention is obvious in the case.  No reason to obscure the obvious things.  If your teacher does not agree, ask "Why?"

Author

Commented:
Dude I need help not a style guide on how to format the code.

Author

Commented:
You are not helping me at all with this post.
CERTIFIED EXPERT

Commented:
Maybe you did not read it all.  I will make it shorter for you ;)

Put "print repr(row)" before or after your .append(row) to see what is pushed into the list.

Author

Commented:
It is printing the contents of the file to the screen
CERTIFIED EXPERT

Commented:
No. It is printing the representation of lists that are put into your list.  You get the list of lists.  This way you have to do something more when getting the row.  You have to extract the wanted element (possibly a single one, I do not know what your file contains).  Then the element taken from the row have to be converted to integer, and the resulting integer have to be appended into the returned list.  

If you call a list "insert" (and the like) then it may be difficult to spot various flaws.

If you want to call yourself megaaps, you have to start with small apps that work ;)  No offense meant.  The way is to think about yourself not being the most clever programmer in the World.  It is neccessary to train your brain a lot.

Author

Commented:
My file contains 200000 integers. All I need to do is get the numbers into a list and be able to get the sum of the elements and and the average. The problem is that the sum() function doesn't work with a list. The elements of the list has to be converted to an integer. And that is the problem. Like I can't even get the sum of all of the numbers in the file. I just need to know how to convert a list to a list of integers and use the vaues
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Ok how to do that? Because when I try to loop through the list I try to slice and index but can't get ints from it
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
James MurrellQA Product Specialist
CERTIFIED EXPERT

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.