For parsing real-world CSV (comma-separated values) data you'll want to use the CSV module that's included with recent versions of Python.
CSV is a set of conventions rather than standard. The sample data you show is simple and regular, but CSV generally has some ugly corner cases for quoting where the contents of any field might have embedded commas, for example.
Here is a very crude program, based on your code, which does naïve parsing of the data (splitting by lines, then splitting each line on commas). It will not handle any data which doesn't split to precisely the correct number of fields, nor any where the numeric fields aren't correctly parsed by the Python int() and float() functions (object constructors). In other words this contains no error checking nor exception handling.
However, I've kept it deliberately simple so it can be easily compared to your rough notes. Also note that I've used the normal Python conventions regarding "self" references in the class definition. (About the only time one would use names other than "self" for these is when doing "meta-class" programming ... writing classes which dynamically instantiate other classes. Any other case will almost certainly cause serious concerns in the minds of any experienced Python programmers looking at your code).
#!/usr/bin/env python
class Jewel:
def __init__(self, name, carat, value):
self.name = name
self.carat = int(carat)
self.value = float(value)
assert self.carat != 0 # Division by zero would result from this
def __repr__(self):
return repr((self.name, self.carat, self.value))
if __name__ == '__main__':
sample='''ruby,2,100
diamond,1,400
emerald,3,250
amethyst,2,50
opal,1,300
sapphire,2,500
malachite,1,60'''
these_jewels = list()
for each_line in sample.split('\n'):
gem_type, carat, value = each_line.split(',')
these_jewels.append(Jewel(gem_type, carat, value))
# Equivalently:
# these_jewels.append(Jewel(*each_line.split(',')))
decorated = [(x.value/x.carat, x) for x in these_jewels]
results = [x[1] for x in sorted(decorated)]
print '\n'.join([str(x) for x in results])
The parsing here is done simply using the string .split() method, and the data is extracted into names using Python's "tuple unpacking" syntax (this would fail if any line of input were to have the wrong number of fields).
The alternative syntax to those two lines uses Python's "apply" syntax. The * prefix on the argument causes it to be unpacked into separate arguments which are passed to the Jewel() class instantiation.
This code also uses the widespread (and widely recommended) DSU (decorate, sort, undecorate) pattern for sorting on some field of your data. I "decorate" the data by creating a series of tuples: (computed value, object reference), then "undecorate" the sorted data in a way which I hope is clear to you. (It would be immediately clear to any experienced Python programmer).
Yes the whole DSU could be reduced to a single line; I've separated it here for legibility and pedagogical purposes.
Again this sample code is purely for your edification. You should use the CSV module on any real-world data; and you should introduce exception handling either in the parsing or in the Jewel.__init__ handling (for converting the numeric data into the correct Python types.
(Also note that you should consider using Python's Decimal module rather than float()s for representing monetary values ... or at least storing the values in cents or mils and using your own functions to represent those as dollars and cents).
self:D