Python:从特定列获取数据的列标题

时间:2015-10-12 16:48:22

标签: python csv tsv field-names

我需要处理一系列以制表符分隔的文本文件。我需要在“研究”栏中获取所有唯一值的标题。

例如: 如果我的数据如下:

csv1:

name   study   id   race
aaa   cb10   123   asian
bbb   cb10   128 
ccc   vj97   864   

CSV2:

name study vaccine
aaa cb10
bbb cb10 abc
ccc vj97 poi

来自多个文件,我的输出应该是'研究'中所有研究的列标题。柱:

cb10- name,study,id,race,vaccine
vj97- name,study,id,vaccine

我有以下代码:

import os
import sys
import glob, ntpath, csv

def get_header_for_tsv_file(tsv_data):
    if not os.path.exists("Results"):
        os.makedirs("Results")

    #output_path = os.path.join ("Results",study + ".csv")

    result = []
    search_for = study
    header = tsv_data.next()
    #output_file = open (output_path, "ab")
    #for row in tsv_data:
    if data["study"] in search_for:
        print data

def path_leaf(path):
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)

def get_tsv_list():
    tsv_list = glob.glob(os.getcwd()+"\*.txt")
    return tsv_list

def get_tsv_data(tsv_name):
    file_name = os.path.join(tsv_name + ".txt")
    if not os.path.exists(file_name):
        print "Error: Couldn't find file:", file_name
        sys.exit(-1)

    input_data = open (file_name)
    input_data = csv.DictReader(input_data, delimiter = "\t")
    return input_data

def run(tsv_name):
    tsv_data = get_tsv_data(tsv_name)
    header_data = get_header_for_tsv_file(tsv_data)

if __name__ == "__main__":
    tsv_list = get_tsv_list()
    filename = [path_leaf(path) for path in tsv_list]
    for index in range(0, len(filename)):
        tsv_name_list = filename[index]
        tsv_name = os.path.splitext(os.path.basename(tsv_name_list))[0]
        tsv_data = get_tsv_data(tsv_name)
        for data in tsv_data:
            study = data["study"]
            run(tsv_name)

我希望使用默认的csv包而不是pandas,如果可能的话。有没有办法可以做到?

1 个答案:

答案 0 :(得分:0)

在伪代码中:

load all file via pandas
take the unique values from the studys - series
make a set from the values above.
Output them