从.csv文件中的某个位置提取值

2024-10-02 10:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧df,它包含一列指向多个csv df['path']的路径。csv如下所示:

# Reaction: a +  94Mo Production of  94Ru Ground state
# Beam current:      0.00250 mA Energy range:   40.000 -->   39.000 MeV
# Irradiation time     :      0 years   0 days  5 hours  0 minutes  0 seconds 
# Cooling time         :      0 years   0 days  0 hours  0 minutes  0 seconds 
# Half life            :      0 years   0 days  0 hours 51 minutes 48 seconds 
# Maximum production at:      0 years   0 days 20 hours 50 minutes 10 seconds 
# Initial production rate:  1.87357E-14 [s^-1] Decay rate:  2.23020E-04 [s^-1]
# # time points =100
# Time [h] Activity [GBq] #isotopes [   ]  Yield [GBq/mAh]  Isotopic frac.
     0.1    9.06448E-05    4.06442E+08    3.62579E-01        0.00355
     0.2    1.74297E-04    7.81528E+08    3.34607E-01        0.00347
     0.3    2.51495E-04    1.12768E+09    3.08792E-01        0.00339

我想提取“半衰期”的价值。在everyfile中,这始终是.csv文件第五行冒号之后的数字

回答: 根据下面的答案,我构造了一个正则表达式来提取值:

for i, p in enumerate(df['path']):
    with open(p, 'r') as f:
            text = open(p)
            for line in text:
                if re.match('# Half life\s*:\s*([^\n]+)', line):
                    number = re.match('# Half life\s*:\s*([^\n]+)', line).group(1)

Tags: csvpathdfratetimelinedaysproduction
1条回答
网友
1楼 · 发布于 2024-10-02 10:32:03

这种模式应该适合你

# Half life\s*:\s*([^\n]+)


它匹配字符串的开头:# Half life

然后,到冒号的可变空格数:\s*:

然后,另一个可变的空格数:\s*

然后,它捕获所有内容,直到一个新行字符:([^\n]+)

您可以访问捕获组1中的值

相关问题 更多 >

    热门问题