0

is there a way to store these input into a dictionary json file so when i import into panda it's easy to analyse? and if this code can be written in an easier way that would also be great (like loop)

#student's profile to be saved in file separately
j = open("jessica.txt",'a')
w = open("wendy.txt", 'a')
t = open("tatiana.txt", 'a')

#user input to record the log
name = input("Name:")
date = input('Enter a date in YYYY-MM-DD format:')
hours = input("Hours:")
rate = input("Rate:")
topic = input('Topic:')

if name == 'Jessica':
    j.writelines("Date:" + date + '\n')
    j.writelines("Hours:" + hours + '\n')
    j.writelines("Rate:" + rate + '\n')
elif name == 'Tatiana':
    t.writelines("Date:" + date + '\n')
    t.writelines("Hours:" + hours + '\n')
    t.writelines("Rate:" + rate + '\n')
else:
    w.writelines("Date:" + date + '\n')
    w.writelines("Hours:" + hours + '\n')
    w.writelines("Rate:" + rate + '\n')
2
  • 1
    Yes, obviously, everything can be stored as json. And yes, refactoring it would be great. What's the exact problem? Do you have any concrete questions, or should it be posted on freelancer.com or on Code Review Stack Exchange? Commented Apr 1, 2018 at 1:30
  • At first you should create the dictionary you want to store. Commented Apr 1, 2018 at 1:40

1 Answer 1

3

Here is an example:

import json

def get_inputs():
    #user input to record the log
    name = input("Name:")
    d =  {}
    d['date'] = input('Enter a date in YYYY-MM-DD format:')
    d['hours'] = input("Hours:")
    return(name,d)

out = {}

while True:
    exit = input('Do you want to add another input (y/n)? ')
    if exit.lower() == 'n':
        break
    else:
        name, d = get_inputs()
        out[name] = d

with open('names.json','w') as f:
    json.dump(out, f, indent=2)

And then:

import pandas as pd
print(pd.read_json('names.json'))

And you have:

          Jessica
date   2014-12-01
hours          12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.