英文:
How to read a string with mixed nature in python
问题
I have taken some data from an instrument. These data contain the temperature with time. A part of the data looks like,
00:00:08
section A 76.21
section B 76.54
section C 76.66
section D 77.87
00:15:30
section A 76.23
section B 76.89
section C 77.98
section D 78.60
…
and the file continues till the end.
00:00:08 means 00 hour, 00 minutes, and 08 seconds.
Now I need to read this data and make a plot of the temperature with time.
My code until now is as follows,
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.dates import DateFormatter
from datetime import datetime
import re
def main():
file = r"C:\Users\sarad\ss1.dat.log"
index = []
month = []
day = []
year = []
hour = []
minute = []
second = []
s1 = []
s2 = []
s3 = []
s4 = []
s5 = []
s6 = []
s7 = []
s8 = []
with open(file) as line:
while True:
result = re.findall(r"(\d+(?:.\d+){0,1})", line.read())
if not result:
break
hour.append(int(result[0]))
minute.append(int(result[1]))
second.append(int(result[2]))
s1.append(float(result[3]))
s2.append(float(result[4]))
s3.append(float(result[5]))
s4.append(float(result[6]))
s5.append(float(result[7]))
s6.append(float(result[8]))
s7.append(float(result[9]))
s8.append(float(result[10]))
I have to use a loop somehow and read all the data.
Besides that, I need to plot the data vs. time. How do I do that in code? I tried with the datetime function, but that is not working.
Could you give me some ideas?
Thank you.
I described it in the previous section.
The Python version is 3.11.
英文:
I have taken some data from a instrument. These data contain the temperature with time. A part of the data looks like,
00:00:08
section A 76.21
section B 76.54
section C 76.66
section D 77.87
00:15:30
section A 76.23
section B 76.89
section C 77.98
section D 78.60
…
and the file continues till end.
00:00:08 means 00 hour, 00 minutes, and 08 seconds.
Now I need to read this data and make a plot of the temperature with the time.
My code till now is following,
import matplotlib.cm as cm
from matplotlib.dates import DateFormatter
from datetime import datetime
import re
def main():
file = r"C:\Users\sarad\ss1.dat.log"
index =[]
month =[]
day =[]
year =[]
hour =[]
minute =[]
second =[]
s1 =[]
s2 =[]
s3 =[]
s4 =[]
s5 =[]
s6 =[]
s7 =[]
s8 =[]
with open(file) as line:
while True:
result=re.findall(r"(\d+(?:.\d+){0,1})",line.read())
if not result:
break
hour.append(int((result[0])))
minute.append(int((result[1])))
second.append(int((result[2])))
s1.append(float(result[3]))
s2.append(float(result[4]))
s3.append(float(result[5]))
s4.append(float(result[6]))
s5.append(float(result[7]))
s6.append(float(result[8]))
s7.append(float(result[9]))
s8.append(float(result[10])) ```
I have to use loop somehow and read all the datas .
Beside that i need to plot the datas vs time . How do i do that in code ? I tried with datetime function but that is not working.
Could you give some idea ?
Thank you.
I described it in the previous section.
The python is 3.11 version.
</details>
# 答案1
**得分**: 1
Here's the translated code part:
```python
你的文本结构看起来非常良好。解析它不应该是问题。以下是我会如何处理它的方式。
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
text = '''
00:00:08
section A 76.21
section B 76.54
section C 76.66
section D 77.87
00:15:30
section A 76.23
section B 76.89
section C 77.98
section D 78.60
'''
table = defaultdict(dict)
for line in text.split('\n'):
if line:
entry = line.strip()
if ':' in entry:
t = entry
else:
_, sample, data = entry.split()
table[t].update({sample:float(data)})
print(table)
df = pd.DataFrame(table).T
print(df)
df.plot()
# 设置刻度标签和旋转
plt.xticks(
ticks=range(len(df)),
labels=df.index,
rotation=45
)
plt.show()
```
Please note that I've only translated the code part, as requested.
<details>
<summary>英文:</summary>
Your text looks very well structured. It shouldn't be a problem to parse it. Here is how I would do it.
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
text = '''
00:00:08
section A 76.21
section B 76.54
section C 76.66
section D 77.87
00:15:30
section A 76.23
section B 76.89
section C 77.98
section D 78.60
'''
table = defaultdict(dict)
for line in text.split('\n'):
if line:
entry = line.strip()
if ':' in entry:
t = entry
else:
_, sample, data = entry.split()
table[t].update({sample:float(data)})
print(table)
#defaultdict(<class 'dict'>, {'00:00:08': {'A': 76.21, 'B': 76.54, 'C': 76.66, 'D': 77.87}, '00:15:30': {'A': 76.23, 'B': 76.89, 'C': 77.98, 'D': 78.6}})
df = pd.DataFrame(table).T
print(df)
# A B C D
#00:00:08 76.21 76.54 76.66 77.87
#00:15:30 76.23 76.89 77.98 78.60
df.plot()
#set ticks labels and rotation
plt.xticks(
ticks = range(len(df)),
labels = df.index,
rotation = 45
)
plt.show()
[![enter image description here][1]][1]
To read from file:
file_name = 'myfile.txt'
with open(file_name, 'r') as f:
for line in f:
entry = line.strip():
if entry:
if line.strip():
if ':' in entry:
t = entry
else:
_, sample, data = entry.split()
table[t].update({sample:float(data)})
...
[1]: https://i.stack.imgur.com/PHQF9.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论