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

  • Home
  • SEARCH
  • 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 8358249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:44:09+00:00 2026-06-09T10:44:09+00:00

Currently, I have a CSV file set out like this: Element,Weight Hydrogen,1 Oxygen,16 Eventually

  • 0

Currently, I have a CSV file set out like this:

Element,Weight
"Hydrogen","1"
"Oxygen","16"

Eventually it’ll have all of the elements and their atomic weights next to them, but putting all of that in is rather pointless unless I get this issue solved first.

The main Python program is as follows:

import csv
reader = csv.reader(open("chem.csv", "rb"))

first = raw_input("Enter first element: ")
second = raw_input("Enter second element: ")

if first == "Hydrogen" or "hydrogen":
     for Weight in reader:
        print Weight

else:
     print "No."

Now, as you might be able to tell from that, my aim here is to have the program display the weight of hydrogen as taken from the CSV file, for now. However, currently it just ends up displaying the following:

Enter first element: hydrogen
Enter second element: oxygen
['Element', 'Weight']
['Hydrogen', '1']
['Oxygen', '16']

So, basically, how can I make it so that it goes to the Hydrogen row and then takes the weight value from the second column? I should be able to go from there to nail the rest of the program, but this part has got me stuck.

As a secondary thing, is it at all possible to have it so that I won’t have to have what is essentially a list of elements in the main Python program as well as in the CSV file? It’d cut down a lot on the clutter, but I just can’t get it figured out.

  • 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-06-09T10:44:11+00:00Added an answer on June 9, 2026 at 10:44 am

    Instead of iterating over the data it is probably nicer to store it in an easy accessible way, e.g. a dictionary mapping name to weight. Then you can just do a look-up for the entered string and display the result (or not if there is none).

    import csv
    
    #Read in data anbd store it in dictionary.
    #The element name will be stored in lowercase.
    elemen_data = { element.lower():int(weight) for element,weight in csv.reader(open("in", "rb"))}
    
    first = raw_input("Enter first element: ")
    second = raw_input("Enter second element: ")
    
    #Look for the weight of the entered element (after converting it to all lowercase)
    weight_first = elemen_data.get(first.lower())
    weight_second = elemen_data.get(second.lower())
    
    if weight_first is not None:
        print 'First Element:', first, 'Weight:', weight_first
    else:
        print 'First Element', first, 'not found'
    
    if weight_second is not None:
        print 'Second Element:', second, 'Weight:', weight_first
    else:
        print 'Second Element', second, 'not found'
    

    Storing your data apart from you program logic can make sense if you want to be able to easily make changes to the data without touching the program. So having a csv-file that is read in is absolutely okay. (As long as the data has a reasonable size)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a CSV file that looks like this: 12345, Here is some
I currently have a csv file that I'm parsing with an example from here:
I'm currently just starting my first Lua program and I have a .csv file
I currently have a CSV that I have built using StringBuilder in C#. This
I currently have a sproc that is exporting data to a csv file using
I currently have a .csv file with several unlabeled columns of data, which to
I have a situation where all records in a CSV I'm parsing are currency,
Currently have password protection on my main and sub directories, however I'd like to
I have a CSV file with the following format: ID | STUFF | Custom
I have a large CSV data file -- ~1,444,000 rows of data -- that

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.