создание функции, позволяющей создать строку заголовка и столбец с именами строк - PullRequest
0 голосов
/ 09 мая 2011

Я определяю функцию, которая будет возвращать список списков, где нулевой элемент - это 2Darray, первый элемент - информация заголовка, а второй - имя строки. Как я могу прочитать это из файла, где

файл выглядит так:

гены S1 S2 S3 S4 S5

100 -0,243 -0,021 -0,205 -1,283 0,411

10000 -1,178 -0,79 0,063 -0,878 0,011

def input2DarrayData(fn):
    # define twoDarray, headerLine and rowLabels
    twoDarray = []
    # open filehandle
    fh = open(fileName)
    # collect header information


    # read in the rest of the data and organize it into a list of lists
    for line in fh:
        # split line into columns and append to array
        arrayCols = line.strip().split('\t')
        # collect rowname information

        **what goes here?**


        # convenient float conversion for each element in the list using the
        # map function. note that this assumes each element is a number and can
        # be cast as a float. see floatizeData(), which gives the explicit
        # example of how the map function works conceptually.
        twoDarray.append(map(float, arrayCols))
    # return data
    return twoDarray

Я продолжаю получать сообщение о том, что оно не может преобразовать первое слово в файле (genes) в число с плавающей точкой, потому что это строка. Поэтому моя проблема в том, чтобы понять, как читать только в этой первой строке

Ответы [ 2 ]

1 голос
/ 09 мая 2011

Чтобы обработать строку заголовка (первую строку в файле), используйте ее явно с .readline() перед итерацией по оставшимся строкам:

fh = open(fileName)
headers = fh.readline().strip().split('\t')
for line in fh:
    arrayCols = line.strip().split('\t')
    ## etc...

Я не уверен, какую структуру данных вы хотите получитьиз файла;Вы, кажется, подразумеваете, что вам нужен список в строке, который включает заголовки.Подобное дублирование заголовков не имеет особого смысла.

Предполагая довольно тривиальную файловую структуру со строкой заголовка и фиксированным числом столбцов в строке, ниже приведен генератор, который выдает словарь на строку, используя заголовкив качестве ключей и значения столбцов в качестве значений:

def process_file(filepath):
    ## open the file
    with open('my_file') as src:
        ## read the first line as headers
        headers = src.readline().strip().split('\t')
        for line in src:
            ## Split the line
            line = line.strip().split('\t')
            ## Coerce each value to a float
            line = [float(col) for col in line]
            ## Create a dictionary using headers and cols
            line_dict = dict(zip(headers, line))
            ## Yield it
            yield line_dict

>>> for row in process_file('path/to/myfile'):
...     print row
>>> 
>>> {'genes':100.00, 'S1':-0.243, 'S2':-0.021, 'S3':-0.205,  'S4': -1.283, 'S5': 0.411}
>>> {'genes':10000.00, 'S1':-1.178, 'S2':-0.79, 'S3':0.063,  'S4': -0.878, 'S5': 0.011}
1 голос
/ 09 мая 2011
def input2DarrayData(fn):
    # define twoDarray, headerLine and rowLabels
    twoDarray = []
    headerLine = None
    rowLabels = []
    # open filehandle
    fh = open(fn)

    headerLine = fh.readline()
    headerLine = headerLine.strip().split('\t')

    for line in fh:
        arrayCols = line.strip().split('\t')
        rowLabels.append(arrayCols[0])

        twoDarray.append(map(float, arrayCols[1:]))
    # return data
    return [twoDarray, headerLine, rowLabels]

Если это работает для вас, пожалуйста, прочитайте PEP-8 и измените имена переменных и функций. Также не забудьте закрыть файл. Лучшее использование with, которое закрывает его для вас:

def input2DarrayData(fn):
    ""
    twoDarray = []
    rowLabels = []
    #
    with open(fn) as fh:
       headerLine = fh.readline()
       headerLine = headerLine.strip().split('\t')
       for line in fh:
           arrayCols = line.strip().split('\t')
           rowLabels.append(arrayCols[0])
           twoDarray.append(map(float, arrayCols[1:]))
    #
    return [twoDarray, headerLine, rowLabels]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...