读取文件,查找子字符串,并将其写入目录

时间:2013-11-12 23:27:38

标签: python string file catalog

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

我知道一般方法;读取文件,找到相关的子字符串,保存该字符串,并用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)

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

2 个答案:

答案 0 :(得分:2)

显然有更优雅的方法可以做到这一点,但这将完成工作,这是你的python旅行和冒险的一个很好的起点。上帝的速度。

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]

答案 1 :(得分:1)

要“读取文件”,如果它是文本文件(大约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]

我们已经完成了。