我想定期运行一个python脚本,每次重复都有一个变量。我该怎么做?

2024-06-08 12:17:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它根据方位移动机器人。计算出的方位是相对于另一个机器人的。我想计算一个校正因子,基于机器人移动的磁轴承+与网络中其他点的距离变化。这个修正系数可以应用于相对于另一个机器人计算的轴承,使轴承更接近真正的磁性轴承(我已经计算出了这背后的数学,但不认为有必要在这里详细讨论)。你知道吗

我的脚本运行的方式是调用其他脚本,并向它们传递值和从中读取值。一段简单的psedou代码如下所示:

find a bearing relative to another bot to the point to be reached
move towards it along this bearing
test accuracy of the bearing
calculate a correction factor

然后我想重复这个脚本并用校正因子(简单的加或减x度)校正最初计算的方位角

如何在每次脚本重复时保留变量,以便在下次添加或减去校正因子,而不必从头开始重新计算?你知道吗


Tags: theto网络脚本距离机器人数学因子
2条回答

将其存储在如下文件中:

import json
json.dump(data, open(filename, 'wb'))

下次用红色

f = open(filename)
data = json.load(f)
f.close()

Json是人类可读的,很好用。在Python中序列化数据的另一个好方法是^{}模块。优点是可以透明地存储almost any Python value。你知道吗

举个例子:

import pickle

def put_persistent(data):
    with open('data.pkl', 'wb') as output:
        pickle.dump(data, output)

def get_persistent(default=None):
    try:
        with open('data.pkl', 'rb') as pkl_file:
            return pickle.load(pkl_file)
    except IOError:
        return default

相关问题 更多 >