将python文本输出传输到google文档

pftdvrlh  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(148)

我正在尝试创建一个情绪跟踪器,其中的python程序会问一些特定的问题,然后会得出一个量化的值,你是多么生气,惊讶,高兴等。我希望该程序的文件的React,并存储在某个地方,我可以访问以后。有没有一种方法,我可以发送输出到一些地方,如谷歌文档?
输出将是如下字符串:

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

等等
就我个人而言,我想过在代码中加入一个字典,但我意识到每次我再次运行代码时它都会重置。

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])
hs1rzwqc

hs1rzwqc1#

你可能想使用write it to a file,你也可以尝试writing it to Google Docs,但它更复杂,而且不是真正为数据存储设计的,你最好的选择是使用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

或者,对于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

泡菜示例:

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

JSON示例:

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

相关问题