python:为每种条目编号

时间:2014-04-22 14:08:21

标签: python sorting file-io formatting

我的数据看起来像这样:

car    trans  +  1,4,6,8
plane  trans  +  3,5,7,9,4,3
train  trans  -  2,4,6,7
bus    trans  -  1,3,4,5,6,7,8

需要按以下格式组织。我基本上想从第4列中取“eventh”数字并将其放在第4列,它是“+”或第5列,如果它是“ - ”。然后,如果它的“+”我想将​​其值加1并将其放在第5列。如果它是“ - ”,我想减去1并将其放在第4列

car.1    trans  +  4  5
car.2    trans  +  8  9
plane.1  trans  +  5  6
plane.2  trans  +  9  10
plane.3  trans  +  3  4
train.1  trans  -  3  4
train.2  trans  -  6  7
bus.1    trans  -  2  3
bus.2    trans  -  4  5
bus.3    trans  -  6  7

以下是我现在的代码。这给出了我想要的输出,但唯一的问题是第一列上的名称没有按我的意愿排序。 (car.1,car.2)我知道我必须将它指向output.write()行,但我不知道如何创建一个字符串来对原始数据中逗号分隔值的元素进行编号。请帮帮我!

import sys
import string
infileName = sys.argv[1]
outfileName = sys.argv[2]

def getGenes(infile, outfile):

    infile = open(infileName,"r")
    outfile = open(outfileName, "w")

    while 1:
       line = infile.readline()
       if not line: break
       wrds = string.split(line)
       comma = string.split(wrds[3], ",")
       fivess = comma[1::2]


    if len(wrds) >= 2:
        name = wrds[0]
        chr = wrds[1]
        type = wrds[2]
        print(type)
    if type == "+":
        for jj in fivess:
            start = jj
            stop = string.atoi(jj)+1
            outfile.write('%s%s\t%s\t%s\t%s\t%s\n' %(name, , chr, type, start, stop))           
    elif type == "-":
        for jj in fivess:
            stop = jj
            start= string.atoi(jj)-1
            outfile.write('%s%s\t%s\t%s\t%s\t%s\n' %(name, ,chr, type, start, stop))   




getGenes(infileName, outfileName)

1 个答案:

答案 0 :(得分:0)

您实际上不必在output.write()行上执行此操作。理想情况下,您应该根据输入进行操作,这样您可以先进行正确的排序,然后在不必考虑顺序的情况下进行处理。这是我编写的代码,使用您的代码作为框架,但澄清/防错一些事情:

import sys

infileName_s = sys.argv[1]
outfileName_s = sys.argv[2]

def getGenes(infileName, outfileName):

    infile = open(infileName,"r")
    outfile = open(outfileName, "w")

    x = infile.read()
    infile.close()   # make sure to close infile and outfile
    data = x.split('\n')
    alldata = []
    for line in data:
        alldata.append(line.split())
        alldata[-1][-1] = alldata[-1][-1].split(',')

    alldata = sorted(alldata) # sort

    mod_alldata = []

    for line in alldata: # create data structures
        for i in range(1, len(line[-1]), 2):
            if line[2] == '+':
                mod_alldata.append([line[0]+'.'+str(i/2+1), line[1], line[2], line[3][i], int(line[3][i])+1])
            else:
                mod_alldata.append([line[0]+'.'+str(i/2+1), line[1], line[2], int(line[3][i])-1, line[3][i]])

    for line in mod_alldata: # write to file
        outfile.write(line[0] + '\t' + line[1]+ '\t' + line[2] + '\t' + str(line[3]) + '\t' + str(line[4]) + '\n')
    outfile.close()

getGenes(infileName_s, outfileName_s)

注意事项:

  • 始终关闭您打开的文件。
  • 请注意变量范围 - 您在函数内外使用了infileName / infileoutfileName / outfile
  • 使用步长为2的range(就像我在这里做的那样:range(1, len(line[-1]), 2))对迭代偶数索引非常有帮助,而且在奇数/空的情况下它也很强大列表。
  • 我使用sorted()按字母顺序排序,因为我不知道你希望它们如何排序。如果您希望它们的排序方式不同,请在评论中告诉我。

这是指定文本文件的输出:

bus.1   trans   -   2   3
bus.2   trans   -   4   5
bus.3   trans   -   6   7
car.1   trans   +   4   5
car.2   trans   +   8   9
plane.1 trans   +   5   6
plane.2 trans   +   9   10
plane.3 trans   +   3   4
train.1 trans   -   3   4
train.2 trans   -   6   7