2013-03-06 66 views
0

所以我有一个程序,它读取一堆文件并追加我需要的必要数据。我现在需要把这些特定数据作为一个列表显示出来。更具体地说,这些是我的参数:在python中创建数据列的列

a =光源,b =光度,c =光度误差,d = HST,e = XRS,f = gmag,g = z,h = rh

我想在列表中显示它,每个列表定义一个特定的列。我只是不知道我应该在我做过的各种for循环中插入print语句。

我将不胜感激任何帮助!这里的程序(主要关注于循环完成以及它们如何遍历数据,不用担心缩进,到目前为止,程序只需要显示附加在列中的数据):

import sys 
    import os 
    import re 
    import urllib 
    import urllib2 
    from os.path import basename 
    import urlparse 
    import shutil 


    base_dirname = '/projects/XRB_Web/apmanuel/499/' 
    base_sourcefile = base_dirname + 'Sources.txt' 

    try: 
     file = open(base_sourcefile, 'r') 
    except IOError: 
     print 'Cannot open: '+base_sourcefile 

    Source = [] 
    Finallist = [] 
    ACS = [] 
    SRC = [] 


    for line in file: 
     data_line_check = (line.strip()) 
     if data_line_check: 

     line = re.sub(r'\s+', ' ', line) 
     point = line.split('|') 
     temp_source = (point[0]).strip() 
     if temp_source and len(point) == 3: 
       Source = (point[0]).strip() 
       Source = re.sub(r'\s', '_', Source) 
       print Source+"\n" 

     temp_finallist = (point[1]).strip() 
     if temp_finallist: 
      Finallistaddress = (point[1]).strip() 
      Finallistaddress = re.sub(r'\s', '_', Finallistaddress) 
      Luminositybase_dirname1 = '/projects/XRB_Web/apmanuel/499/Lists/' + Finallistaddress 


    try: 
     file2 = open(Luminositybase_dirname1, 'r') 
    except IOError: 
     print 'Cannot open: '+Luminositybase_dirname1 

    source = [] 
    luminosity = [] 
    luminosityerr = [] 

    for line in file2: 
     pointy = line.split() 
    a = int(pointy[0]) 
     b = float(pointy[5]) 
     c = float(pointy[6]) 

     source.append(a) 
     luminosity.append(b) 
     luminosityerr.append(c) 

     temp_HST = (point[2]).strip() 
     if temp_HST: 
      HSTaddress = (point[2]).strip() 
      HSTaddress = re.sub(r'\s', '_', HSTaddress) 
      HSTbase_dirname2 = '/projects/XRB_Web/apmanuel/499/Lists/' + HSTaddress 


    try: 
     file3 = open(HSTbase_dirname2, 'r') 
    except IOError: 
     print 'Cannot open: '+HSTbase_dirname2 

    HST = [] 

    for line in file3: 
    pointy2 = line.split() 
    d = int(pointy2[0]) 
     HST.append(d) 

     temp_XRS = (point[3]).strip() 
     if temp_XRS: 
     XRSaddress = (point[3]).strip() 
     XRSaddress =re.sub(r'\s', '_', XRSaddress) 
     XRSbase_dirname3 = '/projects/XRB_Web/apmanuel/499/Lists/' + XRSaddress 


    try: 
     file4 = open(XRSbase_dirname3, 'r') 
    except IOError: 
     print 'Cannot open: '+XRSbase_dirname3 

    XRS = [] 

    for line in file4: 
    pointy3 = line.split() 
    e = int(pointy3[0]) 
     XRS.append(e) 

    temp_others = (point[4]).strip() 
    if temp_others: 
     othersaddress = (point[4]).strip() 
     othersaddress =re.sub(r'\s', '_', othersaddress) 
     othersbase_dirname4 = '/projects/XRB_Web/apmanuel/499/Lists/' + othersaddress 

    try: 
     file5 = open(othersbase_dirname4, 'r') 
    except IOError: 
     print 'Cannot open: '+othersbase_dirname4 

    gmag = [] 
    z = [] 
    rh = [] 

    for line in file5: 
    pointy4 = line.split() 
    f = float(pointy4[3]) 
     g = float(pointy4[5]) 
     h = float(pointy4[7]) 
     rh.append(f) 
     gmag.append(g) 
     z.append(h) 
+0

似乎一个令人难以置信的后退方式,命名变量为'a,b,c,e,f,g ...',然后必须明确地为我们命名它们。为什么不使用变量名称,比如'source,luminosity,luminosity_error,hst,xrs ...'? – msvalkon 2013-03-06 22:57:11

+0

对不起,我只是指出了问题的总体布局。到目前为止,我已经想过在def()语句中使用for循环,然后打印循环,但这只会给我一个混乱。 – 2013-03-06 23:13:25

+0

你有没有考虑过使用[Pandas](http://pandas.pydata.org/)?它可能会简化您的文件扫描代码,让您专注于数据分析。特别是,使用[IO工具](http://pandas.pydata.org/pandas-docs/stable/io.html#csv-text-files)查看关于阅读文本文件的文档。 – crayzeewulf 2013-03-06 23:46:23

回答

0

此函数将返回列的行列表。请注意,这需要列表中的所有元素都在您尝试访问的列中,尽管如果您需要它可以相对简单地进行更改。

def getcolumn(matrix,index):  #index specifies which column of the matrix you want. note that like all other list indexes, this starts from 0, not one. 
    column = [] 
    for row in matrix: 
     column.append(row[index]) 
    return column 
相关问题