Hello everyone im pretty new to python and im having some trouble creating a program that takes a file containing numbers and imports it into python and makes it look like a table. the table is supposed to look something like this when the program is run. basically i figure the best way to do this would be to import the table from excell but i have no idea how to do that, everything else ive found on the net didnt work for me.

Model  :  1   2   3   4   5   6   7   8  :  Avg  Totals
-------------------------------------------------------
  1    :  O   O   2   0   5   6   3   0  :  x.x   xx
  2    :  5   1   9   0   0   2   3   2  :  x.x   xx
  3
  4
  5
  6
  7
  8
  9
 10
--------------------------------------------------------
Average: x.x x.x x.x
Maximum:
Totals :

and this is my program so far it is basically what the professor made up to show columns and rows of numbers
and i cant even get that to work.. ill answer any questions to clarify, sorry if i didn't make enough sense. thanks for anyone who can help me!

# Filename: Houses.py
# Author: 
# Date: 12/01/2012
# Purpose: To show the number of sales in a month,
#          sold by a group of individuals

def intro():
    print()
    print("Program to output a yearly report of house sales.")
    print("Written by .")
    print()

def getfile():
    filename = input("Enter the file name: ") # i receve an error here no matter if i use .xls
    print()                                   # or .txt usually about invalid token or something else when using excell
    infile = open(filename, "r")
    print()
    print("For the file", filename, end="")
    print(":")
    print()
    return infile

def getnumber(infile):
    numbers = []
    i = 0
    for line in infile:
        list = line.split()
        row = []
        for num in list:
            row.append(eval(num))
        numbers.append(row)
        i = i + 1
    print(numbers)
    print()
    return numbers

def printresults(numbers):
    for row in numbers:
        total = 0
        for item in row:
            total = total + item
            print("{0:3}".format(item), end="")
        print("{0:4}".format(total))


def main():
    intro()
    infile = getfile()
    numbers = getnumber(infile)
    printresults(numbers)


main()

Recommended Answers

All 7 Replies

What does your file look like?
Did you save it as a CSV file from excel?

@ZZucker no i tryed saving it as spreadsheet 97-2003 i have excell starter 2010 and it doesnt show the .formats. this is basically what my table looks like in excell

                Salesperson                             
Model   :   1   2   3   4   5   6   7   8   :   Avg Totals
    1   :   0   0   2   0   5   6   3   0   :   2.0 16
    2   :   5   1   9   0   0   2   3   2   :   2.8 22
    3   :   0   0   0   1   0   0   0   0   :   0.1 1
    4   :   1   1   1   0   2   2   2   1   :   1.3 10
    5   :   5   3   2   0   0   2   5   5   :   2.8 22
    6   :   2   2   1   0   1   1   0   0   :   0.9 7
    7   :   3   2   5   0   1   2   0   4   :   2.1 17
    8   :   3   0   7   1   3   5   2   4   :   3.1 25
    9   :   0   2   6   1   0   5   2   1   :   2.1 17
    10  :   4   0   2   0   3   2   1   0   :   1.5 12
Average :   2.3 1.1 3.5 0.3 1.5 2.7 1.8 1.7     1.9 15
Maximum :   5   3   9   1   5   6   5   5           
Totals  :   23  11  35  3   15  27  18  17  :       313

and this is ther error for this one..



Program to output a yearly report of house sales.
Written by .

Enter the file name: housesheet.xls


For the file housesheet.xls:

Traceback (most recent call last):
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 53, in <module>
    main()
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 49, in main
    numbers = getnumber(infile)
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 26, in getnumber
    for line in infile:
  File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 754: character maps to <undefined>

@ZZucker ive also tryed just using the numbers in a .txt file.. like so.

00205630
51900232
00010000
11102221
53200255
22101100
32501204
30713524
02610521
40203210
11102221
53200255

this is also the error i get for this one..

Program to output a yearly report of house sales.
Written by .

Enter the file name: housesheet.txt


For the file housesheet.txt:

Traceback (most recent call last):
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 53, in <module>
    main()
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 49, in main
    numbers = getnumber(infile)
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 30, in getnumber
    row.append(eval(num))
  File "<string>", line 1
    00205630
           ^
SyntaxError: invalid token

i recently solved the .txt. problem implementing commas, but i have recieved a new error, also i still have no idea how to just import the excell spreadsheet into python to use, help on that would be great too. but here is my new error.

Program to output a yearly report of house sales.
Written by .

Enter the file name: housesheet.txt


For the file housesheet.txt:


Traceback (most recent call last):
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 52, in <module>
    main()
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 49, in main
    printresults(numbers,row)
  File "C:/Users/Daryl/Documents/CS 125 Fall 2012/Python projects/cs125/projects/houses/houses.py", line 40, in printresults
    total = total + item
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

also i still have no idea how to just import the excell spreadsheet into python to use,

You most use using something like xldr
A example of use here by Gribouillis.

ive also tryed just using the numbers in a .txt file.. like so.

An example of how to do this,and doing som calulation.

with open('num.txt') as f:
    numb_list = [[float(i) for i in list(i.strip())] for i in f]

for index,numbs in enumerate(numb_list, 1):
    print('{:<2} {} : {:.2f} {}'.format(index,numbs, sum(numbs)/len(numbs), sum(numbs)))

"""Output-->
1  [0.0, 0.0, 2.0, 0.0, 5.0, 6.0, 3.0, 0.0] : 2.00 16.0
2  [5.0, 1.0, 9.0, 0.0, 0.0, 2.0, 3.0, 2.0] : 2.75 22.0
3  [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] : 0.12 1.0
4  [1.0, 1.0, 1.0, 0.0, 2.0, 2.0, 2.0, 1.0] : 1.25 10.0
5  [5.0, 3.0, 2.0, 0.0, 0.0, 2.0, 5.0, 5.0] : 2.75 22.0
6  [2.0, 2.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0] : 0.88 7.0
7  [3.0, 2.0, 5.0, 0.0, 1.0, 2.0, 0.0, 4.0] : 2.12 17.0
8  [3.0, 0.0, 7.0, 1.0, 3.0, 5.0, 2.0, 4.0] : 3.12 25.0
9  [0.0, 2.0, 6.0, 1.0, 0.0, 5.0, 2.0, 1.0] : 2.12 17.0
10 [4.0, 0.0, 2.0, 0.0, 3.0, 2.0, 1.0, 0.0] : 1.50 12.0
11 [1.0, 1.0, 1.0, 0.0, 2.0, 2.0, 2.0, 1.0] : 1.25 10.0
12 [5.0, 3.0, 2.0, 0.0, 0.0, 2.0, 5.0, 5.0] : 2.75 22.0
"""
commented: @snippsat hey thanks for trying to help me, i tryed using your method but i get an error saying cant convert string to float: ',' +0

@snippsat hey thanks for trying to help me, i tryed using your method but i get an error saying cant convert string to float: ','

The file i use num.txt is just a copy of contend in your post.
I have tested code in python 2.7 and 3.2,works fine for me.

commented: yea i think i know why, i have to run the whole program in unix i think its called?, we just call it terminal. it's like a command prompt on linux. im all about ready to just give up for this one haha.. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.