mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
优化数据获取逻辑,处理空数据和异常情况
This commit is contained in:
1 parent
2e033886a5
commit
e7acd57bad
2 files changed
+42
-11
No files matched your search
+18
-9
@@ -24,19 +24,28 @@ def insert_data(time, ph, tds, turbidity):
|
||||
|
||||
# 获取所有数据
|
||||
def get_data():
|
||||
with open(DATA_FILE, "r", newline="", encoding="utf-8") as file:
|
||||
reader = csv.DictReader(file)
|
||||
data = []
|
||||
|
||||
data = []
|
||||
try:
|
||||
with open(DATA_FILE, "r", newline="", encoding="utf-8") as file:
|
||||
reader = csv.DictReader(file)
|
||||
|
||||
for row in reader:
|
||||
row["ph"] = float(row["ph"])
|
||||
row["tds"] = float(row["tds"])
|
||||
row["turbidity"] = float(row["turbidity"])
|
||||
if reader.fieldnames is None:
|
||||
return []
|
||||
|
||||
data.append(row)
|
||||
for row in reader:
|
||||
try:
|
||||
row["ph"] = float(row["ph"])
|
||||
row["tds"] = float(row["tds"])
|
||||
row["turbidity"] = float(row["turbidity"])
|
||||
data.append(row)
|
||||
except (ValueError, TypeError, KeyError):
|
||||
continue
|
||||
|
||||
return data
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
return data
|
||||
|
||||
# 获取最新数据
|
||||
def get_last_data():
|
||||
|
||||
Reference in new issue
Block a user