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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:24:32+00:00 2026-05-25T19:24:32+00:00

I set up a dictionary, and filled it from a file, like so: filedusers

  • 0

I set up a dictionary, and filled it from a file, like so:

filedusers = {} # cheap way to keep track of users, not for production
FILE = open(r"G:\School\CS442\users.txt", "r")
filedusers = ast.literal_eval("\"{" + FILE.readline().strip() + "}\"")
FILE.close()

then later I did a test on it, like this:

if not filedusers.get(words[0]):

where words[0] is a string for a username, but I get the following error:

‘str’ object has no attribute ‘get’

but I verified already that after the FILE.close() I had a dictionary, and it had the correct values in it.

Any idea what’s going on?

  • 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-25T19:24:32+00:00Added an answer on May 25, 2026 at 7:24 pm

    literal_eval takes a string, and converts it into a python object. So, the following is true…

    ast.literal_eval('{"a" : 1}')
    >> {'a' : 1}
    

    However, you are adding in some quotations that aren’t needed. If your file simply contained an empty dictionary ({}), then the string you create would look like this…

    ast.literal_eval('"{}"') # The quotes that are here make it return the string "{}"
    >> '{}'
    

    So, the solution would be to change the line to…

    ast.literal_eval("{" + FILE.readline().strip() + "}")
    

    …or…

    ast.literal_eval(FILE.readline().strip())
    

    ..depending on your file layout. Otherwise, literal_eval sees your string as an ACTUAL string because of the quotes.

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

Sidebar

Related Questions

I'm from a Flash ActionScript background. Where I'm from, I can set a dictionary-like
I have a ConcurrentDictionary object that I would like to set to a Dictionary
Is it possible to set code behind a resource dictionary in WPF. For example
Python has an ordered dictionary . What about an ordered set?
I'm trying to map a Dictionary containing Lists. I have the following set of
Please help me out... I need to set a dictionary as datasource to a
I am writing a method that's intended to return a dictionary filled with configuration
Is it possible to set a dictionary object as a property of a class
Is there an out-of-the-box generic collection type for .Net that combines Set and Dictionary
I've set up a custom namespace lookup dictionary in order to map elements in

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.