Search code examples
pythondictionarygoogle-docs

Transferring python text output onto a google document


I am trying to create a mood tracker in which the python program will ask about certain question and then will come up with a quantitative value of how angry, surprised, happy, etc you are. I want the program to document the response and store it somewhere that I can access later on. Is there a way I can send the output to some place like a google document?

The output would be a string like:

"12/12/22 - Happy': 0.67, 'Angry': 0.0, 'Surprise': 0.33"
"12/13/22 - Happy': 0.58, 'Angry': 0.05, 'Surprise': 0.63"

etc

Personally, I thought about a dictionary within the code, but I realised it just resets every time I run the code again...

class Documentation:
    def __init__(self, date, time, notes):
        self.date = date
        self.time = time
        self.notes = notes
        #self.journal = journal
    global journal
    journal = []  # list
    def time(self):
        now = datetime.datetime.now()
        print(now.year, now.month, now.day, now.hour, now.minute)
    def document(self, date,time, notes):
        journal.append((date, time, notes))
        print(journal)
    def allDocument(self, date, time, notes):
        for i in range(len(journal)):
            print(journal[i])

Solution

  • You probably want to write it to a file. You could try writing it to Google Docs but that's more complicated and not really designed for data storage. Your best bet is to do something like this with pickle:

    import pickle
    
    def save(filename, object):
        with open(filename, "wb") as file:
            pickle.dump(object, file)
    
    def read(filename):
        with open(filename, "rb") as file:
            content = pickle.load(file)
        return content
    

    Or, with JSON:

    import json
    
    def save(filename, object):
        with open(filename, "wt") as file:
            json.dump(object, file)
    
    def read(filename):
        with open(filename, "rt") as file:
            content = json.load(file)
        return content
    

    Example with pickle:

    >>> save("data.bin", 123)
    >>> read("data.bin")
    123
    

    Example with JSON:

    >>> save("data.json", "foo")
    >>> read("data.json")
    'foo'