Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4320946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:41:21+00:00 2026-05-21T08:41:21+00:00

I am trying to read in a CSV file that looks like this: ruby,2,100

  • 0

I am trying to read in a CSV file that looks like this:

ruby,2,100
diamond,1,400
emerald,3,250
amethyst,2,50
opal,1,300
sapphire,2,500
malachite,1,60

Here is some code I have been experimenting with.

class jewel:
    def __init__(gem, name, carat, value):
            gem.name = name
            gem.carot = carat
            gem.value = value
    def __repr__(gem):
            return repr((gem.name, gem.carat, gem.value))

jewel_objects = [jewel('diamond', '1', 400),
                 jewel('ruby', '2', 200),
                 jewel('opal', '1', 600),
                ]

aList = [sorted(jewel_objects, key=lambda jewel: (jewel.value))]
print aList

I would like to read in the values and assign them to name, carat, and value but I’m not sure how to do so. Then once I get them read in I would like to sort them by value per carat so value/carat. I have done quite a bit of searching and have came up blank. Thank you very much for your help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:41:22+00:00Added an answer on May 21, 2026 at 8:41 am

    You need to do two things here, the first is actually loading the data into the objects. I recommend you look at the ‘csv’ module in the standard python library for this. It’s very complete and will read each row and make it easily accessable

    CSV docs: http://docs.python.org/library/csv.html

    I would create a list of the objects, and then implement either an cmp function in your object, or (if you’re using an older version of python) you can pass a function to sorted() that would define it. You can get more info about sorting in the python wiki

    Wiki docs: http://wiki.python.org/moin/HowTo/Sorting

    You would implement the cmp function like this in your class (this can be made a bit more efficent, but I’m being descriptive here)

    def __cmp__(gem, other):
        if (gem.value / gem.carot) < (other.value / other.carot):
            return -1
        elif (gem.value / gem.carot) > (other.value / other.carot): 
            return 1
        else:
            return 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read data from a.csv file to ouput it on a webpage
I'm trying to read a .doc file into a database so that I can
I'm trying to read in a (tab separted) csv file in R. When I
I can't create an utf-8 csv file in Python. I'm trying to read it's
To simplify, I'm trying to read the content of a CSV-file using the ifstream
I'm trying to create a simple class to read a csv file and store
I am trying to read a csv file from assets/file.csv, but the log keeps
I'm trying to read time series from CSV file and save them as xts
I'm trying to read a CSV with R from a URL that has auth
I'm trying to use the csv module to read a utf-8 csv file, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.