首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从以Python中的字符串开头的文本文件的特定行中提取数字

从以Python中的字符串开头的文本文件的特定行中提取数字
EN

Stack Overflow用户
提问于 2021-01-19 18:25:35
回答 1查看 763关注 0票数 0

我正在编写一个代码,在其中我需要从文本文件中读取数据,并且只从其中提取特定的数字。我尝试过numpy loadtxt和genfromtxt,但仍然难以找到答案。下面是一个我需要从其中提取数据的文件的示例:

代码语言:javascript
运行
复制
    #
    #Text
    #
    # =============================
    # Text
    # Text
    # Text
    # Text
    # Text
    # =============================
    /gps/ene/type Arb
    /gps/hist/type arb
    /gps/hist/point   9.200000E-04   1.835400E+11
    /gps/hist/point   1.200000E-03   1.577700E+11
    /gps/hist/point   1.600000E-03   1.348800E+11
    /gps/hist/point   2.100000E-03   1.137300E+11
    /gps/hist/point   2.700000E-03   9.759600E+10
    /gps/hist/point   3.500000E-03   8.348800E+10
    /gps/hist/point   4.500000E-03   6.868300E+10
    /gps/hist/point   5.900000E-03   5.317300E+10
    /gps/hist/point   7.700000E-03   3.917900E+10
    /gps/hist/point   1.000000E-02   2.723300E+10
    /gps/hist/point   1.300000E-02   1.762600E+10
    /gps/hist/point   1.700000E-02   1.058500E+10
    /gps/hist/point   3.000000E-02   3.548700E+09
    /gps/hist/point   6.100000E-02   1.274400E+09
    /gps/hist/point   8.900000E-02   5.074400E+08
    /gps/hist/point   1.300000E-01   1.705900E+08
    /gps/hist/point   1.800000E-01   7.722800E+07
    /gps/hist/point   2.700000E-01   2.671600E+07
    /gps/hist/point   4.000000E-01   9.265800E+06
    /gps/hist/point   6.100000E-01   2.225000E+06
    /gps/hist/point   9.100000E-01   6.600100E+05
    /gps/hist/point   1.280000E+00   2.264300E+05
    /gps/hist/point   1.990000E+00   4.632400E+04
    /gps/hist/point   2.440000E+00   1.928300E+04
    /gps/hist/point   3.070000E+00   6.847300E+03
    /gps/hist/point   3.970000E+00   1.584400E+03
    /gps/hist/point   5.200000E+00   0.000000E+00

    #
    #Normalisation
    #
    /control/alias  NORM_FACTOR_SPECTRUM "   2.945935E+17 "
    /control/alias  NORM_FACTOR_ANGULAR "   2.500000E-01 "

我只需要得到以"/gps/hist/point“开头的行后的数字,并将它们放在一个二维数组中。有什么解决办法吗?

事先非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-19 18:29:28

代码语言:javascript
运行
复制
myList = []

lines = [line for line in open('myfile.txt', 'r')]
for line in lines:
    if line.startswith('/gps/hist/point'):
        myList.append([line.split()[1], line.split()[2])
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65797327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档