如何在Python中读取混合性质的字符串

huangapple go评论94阅读模式
英文:

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&#39;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 = &#39;&#39;&#39;
    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
    &#39;&#39;&#39;
    
    table = defaultdict(dict)
    for line in text.split(&#39;\n&#39;):
        if line:
            entry = line.strip()
            if &#39;:&#39; in entry:
                t = entry
            else:
                _, sample, data = entry.split()
                table[t].update({sample:float(data)})
    
    print(table)
    #defaultdict(&lt;class &#39;dict&#39;&gt;, {&#39;00:00:08&#39;: {&#39;A&#39;: 76.21, &#39;B&#39;: 76.54, &#39;C&#39;: 76.66, &#39;D&#39;: 77.87}, &#39;00:15:30&#39;: {&#39;A&#39;: 76.23, &#39;B&#39;: 76.89, &#39;C&#39;: 77.98, &#39;D&#39;: 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 = &#39;myfile.txt&#39;
    with open(file_name, &#39;r&#39;) as f:
        for line in f:
            entry = line.strip():
            if entry:
    
                if line.strip():
                    if &#39;:&#39; in entry:
                    t = entry
                else:
                    _, sample, data = entry.split()
                    table[t].update({sample:float(data)})
    ...


  [1]: https://i.stack.imgur.com/PHQF9.png

</details>



huangapple
  • 本文由 发表于 2023年7月6日 18:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定