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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:19:46+00:00 2026-05-26T17:19:46+00:00

I am reading from a file with data like this: {day :Monday, alarm:on, kids:School

  • 0

I am reading from a file with data like this:

{"day" :"Monday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Tuesday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Wednesday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Thursday", "alarm":"on", "kids":"School" , "work":"nights"}
{"day" :"Friday", "alarm":"on", "kids":"School" , "work":"nights"}
{"day" :"Saturday", "alarm":"off", "kids":"Dance" , "work":"overtime"}
{"day" :"Sunday", "alarm":"off", "kids":"Soccer" , "work":"off"}

I am putting the data into an dictionary, then evaluating the dictionary for some condition and placing that dictionary into another dictionary like so:

import ast
o=open('schedule.txt','rb')
day_={}
for lines in o:
    dict_={}
    dict_= ast.literal_eval(lines)
    if dict_['day']=='Monday':
        day_['1']=dict_.items()
    elif dict_['day']=='Tuesday':
        day_['2']=dict_.items()
    elif dict_['day']=='Wednesday':
        day_['3']=dict_.items()
    elif dict_['day']=='Thursday':
        day_['4']=dict_.items()
    elif dict_['day']=='Friday':
        day_['5']=dict_.items()
    elif dict_['day']=='Saturday':
        day_['6']=dict_.items()
    elif dict_['day']=='Sunday':
        day_['7']=dict_.items()
    else:
        print('there was an error')
o.close()
print day_.items()
#this seems to work properly

Now if I only want to find out what the kids are doing on day 4, how do I do this?
Or is there an easier way to hold the data for future reference within the program?

  • 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-26T17:19:46+00:00Added an answer on May 26, 2026 at 5:19 pm

    You can simplify your code by using a dictionary to map weekday names to numbers. To extract the "kids" item of the record for day 4, you can use result[4]["kids"]:

    days = {"Monday": 1, "Tuesday": 2, "Wednesday": 3, "Thursday": 4,
            "Friday": 5, "Saturday": 6, "Sunday": 7}
    result = {}
    with open('schedule.txt', 'rb') as f:
        for line in f:
            d = ast.literal_eval(line)
            result[days[d["day"]]] = d
    print result[4]["kids"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading data from a file like this while (<$fh>) { @tmp =
How do I read data from a file if my file is like this
Currently, I am reading data from a binary file (File.ReadAllBytes), converting this byte array
I'm reading positional records from a text file, for examle, it looks like this:
I'm reading from a binary data file which is written by invoking the following
Why does reading from __DATA__ work and reading from the file doesn't (Loose unescaped
I'm reading data from a file -> saving into DB -> Loading my Custom
I am reading data from a file that has, unfortunately, two types of character
I'm having some trouble reading data from a file into a vector of Orders.
I am reading in data from a file that has three columns. For example

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.