Ok I have a file like that looks like this.
panite,1,1800
ruby,2,100
diamond,0.75,900
emerald,3,250
amethyst,2,50
opal,1,300
sapphire,0.5,750
benitoite,1,2000
malachite,1,60
Our teacher gave us code that uses a try/except to help us open the file. I need to open the file and read each line and make each line a tuple and then put it in a list. The list is supposed to be the last numbe divided by the middle number, and then that value followed by the name of the gem(the middle number is the carat of the gem). The problem i'm having is I cant even get it to make a list from the file. This is what i've tried to open it without much success.
def main():
fileFound = False
while not fileFound:
fileName = input('File name containing jewel data: ')
try:
dataFile = open(fileName, "r")
fileFound = True
knapsack()
except:
print ('Could not find that file -- try again')
def knapsack():
list = dataFile.readline()
I've actually had a little success when I changed it to a simple printstatement under def knapsack() where it will print something simple like 2+2, but when I try to make a list, it gives me the except error instead. This is my first programming class so any insight into this would be appreciated.
Use the csv module to read lines as csv rows.
This will give you
output_data
as:which is a list of tuples. This solves your problem of making a list from the file. Now, you should do:
int
before you do the arithmetic operations as you mention in your description.output_data
as an argument to a separate function that does the arithmetic functions you mentioned in your question. This function should build your output list.A few remarks on your code:
You define the file handle in the main function, but do not pass it to the
knapsack
function. But you reference it in the knapsack function which will not give you what you want. So you need to pass thedatafile
file handle as argument to yourknapsack
function. You do this by replacing this line in yourmain
method from:to
list
is the name of a built in type in Python. So it is wise not to use it as the name of a variable in your code.If I understand your question correctly, the exception is because
dataFile
variable is not found inside theknapsack
function. I'd suggest learning scoping rules in Python or read this excellent to-the-point chapter on the topic (or search for this topic on the Web!).The other thing I'd recommend is not to handle all exceptions as you've shown in your snippet. Here's why.
The fixed code could look something like this:
I would go with something more Pythonic using list comprehensions
Where you are passing to your knapsack function the path to your file.
You should:
dataFile
to yourknapsack
function.except
toexcept IOError
to avoid catching exceptions you do want to see.close the file (consider using
with
to open the file to avoid having to close it explicitlyIf you had used
except IOError
from the start of, you would have seen this error:knapsack
does not know whatdataFile
is, hence the error. Always catch specific exceptions when usingtry..except
. If you don't know which error is thrown - reproduce it before you write the code in the python interpreter (e.g try to open a file that doesn't exists, and notice thatIOError
is thrown).In
knapsack
you need to change fromreadline
toreadlines
.You should also consider to use the
csv
module as mentioned in the other answer to handle the data.and some comments:
except:
without a specified exception-type, also known as a "bare exception", is frowned on because it catches everything. You should specify the type of exceptions you expect to see, and only handle those; if you catch everything and something totally unexpected fails (ieComputerOnFireError
!) you will never find out about it.opening a file using
with
is preferred because it ensures the file will always get closed properly.when you open a file in text mode, you can iterate over it line-by-line; this is the most common way to process a file.
when you
.split()
a string, you get a list of strings back. Before doing math on the pieces you have to convert them from a string to a numeric value usingint()
orfloat()
.Hope that helps.