帮我写一段python程序,读写指定的指标,记录格式如 ABC=123.45, 读入或写ABC,写需要覆盖
时间: 2023-02-12 19:57:30 浏览: 83
```python
import os
def read_indicator(indicator_name):
with open("indicator.txt", "r") as f:
for line in f:
if line.startswith(indicator_name):
return line.strip().split("=")[-1]
return None
def write_indicator(indicator_name, value):
indicators = []
if os.path.exists("indicator.txt"):
with open("indicator.txt", "r") as f:
indicators = f.readlines()
with open("indicator.txt", "w") as f:
indicator_written = False
for indicator in indicators:
if indicator.startswith(indicator_name):
f.write(f"{indicator_name}={value}\n")
indicator_written = True
else:
f.write(indicator)
if not indicator_written:
f.write(f"{indicator_name}={value}\n")
# 使用示例
write_indicator("ABC", 123.45)
print(read_indicator("ABC"))
```
这段程序实现了读写指定指标的功能,记录格式为 "ABC=123.45",读入或写入指标时会覆盖原有的值。