• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Need to pull numbers from a file and get the sum

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

0
megaapps
Asked:
megaapps
3 Solutions
 
xelousCommented:
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.
0
 
peprCommented:
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?"
0
 
megaappsAuthor Commented:
Dude I need help not a style guide on how to format the code.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
megaappsAuthor Commented:
You are not helping me at all with this post.
0
 
peprCommented:
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.
0
 
megaappsAuthor Commented:
It is printing the contents of the file to the screen
0
 
peprCommented:
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.
0
 
megaappsAuthor 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
0
 
peprCommented:
I understand your problem very well.  I also understand, that it is your homework...  You decided (or you are forced to) to read the file through the csv.reader().  Because of that you get each line of the file as the list named row.  Even if the line contains a single number, you will get the list with the single element.  The element is of the string type.  Say the file contained numbers 1, 2, 3 -- each on separate line.  The "row" variable contains the lists like ['1'], then ['2'], and then ['3'].  When you append the rows into your "insert" list, you get (i.e. your loadFile() function returns):

[['1'], ['2'], ['3']]

But you want to get

[1, 2, 3]

What I am telling you that the easiest way to get the wanted list of all integers is based on not appending the rows, but appending the integers extracted from the rows.  It means that you want to extract the '1' out of the ['1'], then you want to convert '1' into 1 (via int('1') as xelous suggested above), and finally to append the integer result.
0
 
peprCommented:
There is a bug in your open('Alpha.txt', 'rU').  The csv.reader() requires the file opened in binary mode.  You have to use the 'rb' instead of 'rU'.
0
 
megaappsAuthor 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
0
 
peprCommented:
Lists in Python can be indexed (based on zero) the same way as arrays in other languages.  If row is the list, then row[0] is the first element of the list.  If row is equal to ['1'], then row[0] is equal to '1'.  Just do not append all the row.  Get the row[0], convert it to int, and append the result.

Are you forced to use csv.reader()?  Reading the lines with the numbers would be easier in this case.

0
 
James MurrellProduct SpecialistCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now