I'm trying to read a txt file with informations about time, temperature and humidity, this is the shape
07:54:03.383 -> Humidity:38.00%;Temperature:20.50°C;Heat index:19.60°C;
07:59:03.415 -> Humidity:37.00%;Temperature:20.90°C;Heat index:20.01°C;
08:04:03.435 -> Humidity:37.00%;Temperature:20.90°C;Heat index:20.01°C;
08:09:03.484 -> Humidity:37.00%;Temperature:20.80°C;Heat index:19.90°C;
I would like to extrapolate, for each line, the 4 informations and plot them in a graph. using open() and fileObject.read() i can plot the txt into VSC Terminal, but i don't know how to:
Assuming you can tolerate reading your data into a Python string, we can use re.findall
here:
# -*- coding: utf-8 -*-
import re
inp = """07:54:03.383 -> Humidity:38.00%;Temperature:20.50°C;Heat index:19.60°C;
07:59:03.415 -> Humidity:37.00%;Temperature:20.90°C;Heat index:20.01°C;
08:04:03.435 -> Humidity:37.00%;Temperature:20.90°C;Heat index:20.01°C;
08:09:03.484 -> Humidity:37.00%;Temperature:20.80°C;Heat index:19.90°C;"""
vals = re.findall(r'^(\d{2}:\d{2}:\d{2}(?:\.\d+)?) -> Humidity:(\d+(?:\.\d+)?)%;Temperature:(\d+(?:\.\d+)?)°C;Heat index:(\d+(?:\.\d+)?)°C;', inp, flags=re.M)
print(vals)
This prints:
[('07:54:03.383', '38.00', '20.50', '19.60'),
('07:59:03.415', '37.00', '20.90', '20.01'),
('08:04:03.435', '37.00', '20.90', '20.01'),
('08:09:03.484', '37.00', '20.80', '19.90')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.