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 8716415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:04:18+00:00 2026-06-13T06:04:18+00:00

I am attempting to run a python program that can run a dictionary from

  • 0

I am attempting to run a python program that can run a dictionary from a file with a list of words with each word given a score and standard deviation. My program looks like this:

theFile = open('word-happiness.csv' , 'r')

theFile.close()

def make_happiness_table(filename):
   '''make_happiness_table: string -> dict
      creates a dictionary of happiness scores from the given file'''
   with open(filename) as f:
      d = dict( line.split('    ')  for line in f)
   return d

make_happiness_table("word-happiness.csv")

table = make_happiness_table("word-happiness.csv")
(score, stddev) = table['hunger']
print("the score for 'hunger' is %f" % score)

My .csv file is in the form

word{TAB}score{TAB}standard_deviation

and I am trying to create the dictionary in that way. How can I create such a dictionary so that I can print a word such as ‘hunger’ from the function and get its score and std deviation?

  • 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-13T06:04:19+00:00Added an answer on June 13, 2026 at 6:04 am
    def make_happiness_table(filename):
       with open(filename) as f:
          d = dict()
          for line in f:
             word,score,std = line.split() #splits on any consecutive runs of whitspace
             d[word]=score,std # May want to make floats:  `d[word] = float(score),float(std)`
       return d
    

    Note that if your word can have a tab character in it, but you’re guaranteed that you only have 3 fields (word, score, std), you can split the string from the right (str.rsplit), only splitting twice (resulting in 3 fields at the end). e.g. word,score,std = line.rsplit(None,2).

    As mentioned in the comments above, you can also use the csv module to read these sorts of files — csv really shines if your fields can be “quoted”. e.g.:

    "this is field 0" "this is field 1" "this is field 2"
    

    If you don’t have that scenario, then I find that str.split works just fine.


    Also, unrelated, but your code calls make_happiness_table twice (the first time you don’t assign the return value to anything). The first call is useless (all it does is read the file and build a dictionary which you can never use). Finally, opening and closeing theFile at the beginning of your script is also just a waste since you don’t do anything with the file there.

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

Sidebar

Related Questions

I am attempting to run a python program that can run a dictionary from
I am attempting to run some unit tests in python from what I believe
I'm attempting to run a file that calls multiple files, but I'm getting some
I am attempting to install libxml2 so that I can setup the python bindings
I'm attempting to run the soaplib Hello World program: import soaplib from soaplib.core.service import
I am attempting to run a number of student FORTRAN programs from a python
From the Django shell (manage.py shell), when attempting to import a python module that
I am attempting to wrap some C code in Python using Pyrex. I've run
When attempting to run the following assembly program: .globl start start: pushq $0x0 movq
I am attempting to run a command as root from a php page, is

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.