【问题标题】:Reading in a file, finding a substring, and writing it out to a catalog读入一个文件,找到一个子串,然后把它写到一个目录中
【发布时间】:2013-11-12 23:27:37
【问题描述】:

这是一个我想读入的文件,名为“galfit.365”。我需要通过python访问这个文件,在“输入菜单文件:”后面找到数字“10”,找到数字“39” “在”之后。之后,我想将这些数字保存到目录中。

我知道一般方法;读入文件,找到相关的子字符串,保存该字符串,然后用 asciitable 写出来。但是,我有点像 Python 新手,所以我不知道从哪里开始。首先,我如何“读入文件”?之后,如何找到我感兴趣的特定字符串值?

#  Input menu file: 10f160w39.feedme

#  Chi^2/nu = 0.003,  Chi^2 = 45.157,  Ndof = 16768

================================================================================
# IMAGE and GALFIT CONTROL PARAMETERS
A) NOISE10thousandthsciPHOTOF160w39.fits      # Input data image (FITS file)
B) NOISE10thousandthgalsciPHOTOF160w39.fits      # Output data image block
C) NOISE10thousandthsigmaPHOTOF160w39.fits      # Sigma image name (made from data if blank or "none") 
D) arjen_psf_f160w.fits          # Input PSF image and (optional) diffusion kernel
E) 1                   # PSF fine sampling factor relative to data 
F) maskNOISE10thousandthsciPHOTOF160w39.fits      # Bad pixel mask (FITS image or ASCII coord list)
G) none                # File with parameter constraints (ASCII file) 
H) 1    130  1    130  # Image region to fit (xmin xmax ymin ymax)
I) 200    200          # Size of the convolution box (x y)
J) 26.563              # Magnitude photometric zeropoint 
K) 0.038  0.038        # Plate scale (dx dy)   [arcsec per pixel]
O) regular             # Display type (regular, curses, both)
P) 0                   # Choose: 0=optimize, 1=model, 2=imgblock, 3=subcomps

# INITIAL FITTING PARAMETERS
#
#   For component type, the allowed functions are: 
#       sersic, expdisk, edgedisk, devauc, king, nuker, psf, 
#       gaussian, moffat, ferrer, and sky. 
#  
#   Hidden parameters will only appear when they're specified:
#       Bn (n=integer, Bending Modes).
#       C0 (diskyness/boxyness), 
#       Fn (n=integer, Azimuthal Fourier Modes).
#       R0-R10 (coordinate rotation, for creating spiral structures).
#       To, Ti, T0-T10 (truncation function).
# 
# ------------------------------------------------------------------------------
#   par)    par value(s)    fit toggle(s)    # parameter description 
# ------------------------------------------------------------------------------

# Component number: 1
 0) sersic                 #  Component type
 1) 65.6794  64.2952  1 1  #  Position x, y
 3) 23.7585     1          #  Integrated magnitude 
 4) 0.0100      1          #  R_e (effective radius)   [pix]
 5) 1.6931      1          #  Sersic index n (de Vaucouleurs n=4) 
 6) 0.0000      0          #     ----- 
 7) 0.0000      0          #     ----- 
 8) 0.0000      0          #     ----- 
 9) 0.6379      1          #  Axis ratio (b/a)  
10) -2.3601     1          #  Position angle (PA) [deg: Up=0, Left=90]
 Z) 0                      #  Skip this model in output image?  (yes=1, no=0)

# Component number: 2
 0) sky                    #  Component type
 1) 6.853e-05      1       #  Sky background at center of fitting region [ADUs]
 2) 0.000e+00      0       #  dsky/dx (sky gradient in x)     [ADUs/pix]
 3) 0.000e+00      0       #  dsky/dy (sky gradient in y)     [ADUs/pix]
 Z) 0                      #  Skip this model in output image?  (yes=1, no=0)

================================================================================

【问题讨论】:

    标签: python string file catalog


    【解决方案1】:

    显然有更优雅的方法可以做到这一点,但这将完成工作,它是您的蟒蛇旅行和冒险的一个很好的起点。神速。

    with open("galfit.365") as file:
         lines = file.readlines() # all your lines are here
    
         # should print "#  Input menu file: 10f160w39.feedme"
         # since it is the first line in the file
         print lines[0]
    
         index_of_w = lines[0].index('w')
    
         # this should give you 39
         number_after_w = lines[0][index_of_w+1] + lines[0][index_of_w+2]
    

    【讨论】:

    • 这似乎是一种获取子字符串的便捷方式;非常感谢!
    【解决方案2】:

    要“读取文件”,如果它是一个文本文件(人类可读的大约 80 个字符的行),您通常会逐行执行。看起来像这样:

    with open(filename) as file:
        for line in file:
            # whatever you wanted to do with each line
    

    但在您的情况下,您只想阅读第一行,而不关心其他任何内容,对吗?所以:

    with open(filename) as file:
        first_line = next(file)
        # whatever you wanted to do with first_line
    

    现在,你如何处理那条线?很多人会建议使用正则表达式,\s(\d+)f\d+w(\d+)\. 应该这样做,但您不太可能理解。

    一种方法是找到“拆分”线路以获得所需部分的位置。

    #  Input menu file: 10f160w39.feedme
    

    如果你用空格隔开,你关心的一切都在最后一个字,10f160w39.feedme。 (最后也可能有不可见的空格——事实上,至少有一个换行符。我会用strip先把它们扔掉。)所以,让我们开始吧:

    menufile = line.strip().rsplit()[-1]
    

    现在,f 之前的所有内容都是一个数字,w. 之间的所有内容都是另一个数字,对吧?所以:

    f = menufile.partition('f')[0]
    w = menufile.rpartition('.')[0].rpartition('w')[-1]
    

    我们已经完成了。

    【讨论】:

    • 如果我真的想读入同一个文件的后面几行怎么办?例如,我需要在第 42 行读取 ("3) 23.7585 1 # 积分幅度" ),只写出 23.7585。我将如何修改我的方法来解决这个问题?
    • @vdogsandman:您使用我在顶部显示的for 循环逐行浏览文件。
    • 啊,这是有道理的。谢谢好心的先生!
    • 我得到这个:1 以 open('galfit.333') 作为文件:2 用于文件中的行:----> 3 menufile = line.strip().rsplit() [-1] 4 f = menufile.partition('f')[0] 5 w = menufile.rpartition('.')[0].rpartition('w')[-1] IndexError: 列表索引超出范围有什么问题?
    • @vdogsandman:嗯,该代码仅适用于第一行。第二行是空的。所以,line.strip().rsplit() 是一个空列表,[],所以上面的[-1] 引发了一个IndexError。您需要编写一些“if”语句来识别您何时在特定行上——例如,只做第一行代码if 'Input menu file:' in line,只做综合幅度代码if 'Integrated magnitude' in line,等等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多