2014-04-21 138 views
0

我是一个完整的Python新手。如果你能帮助我,那将会很棒。我的数据格式有点像这样。如果有人能帮助我,我将不胜感激。以逗号分隔,制表符分隔格式组合数据

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 

在逗号分隔值,我想只提取“eventh”号(第2,第4,第6,第8,第10,等)出来,并主张其按+或 - 第三的价值柱。

我想从逗号分隔的数据中放置“eventh”数字,如果它是“+”,则数字转到第四列并将该值加1,然后将其放置在第五列中。如果是“ - ”,则该数字将变为第五列并减去该值,并将其放在第四列中。我知道这真的是非常重要的解释,但如果有人能给我一个我可以从哪里开始的想法,那将是非常好的。谢谢

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 

edit2:所以经过大家的搜索和帮助,我现在有这样的事情。这给了我适当的输出,但我现在唯一的问题是,我无法正确命名它。 (即car.1,car.2,car.3,plane.1,plane.2 ....等)有人能让我洞察这个问题吗?

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], ",") 
    print(comma) 
    fivess = comma[1::2] 
    print(fivess) 

    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\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\t%s\t%s\t%s\t%s\n' %(name, chr, type, start, stop)) 



getGenes(infileName, outfileName) 

回答

0

按标签分割每一行;然后在逗号分隔最后一项(数字列表)。这会给你所有的位来做你的处理。

0

可以使用分割法,以做到这一点:

txt = """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""" 
lines = txt.split("\n") 
for line in lines: 
    vehicle,vehicle_type,action,numbers = line.split('\t') 
    numbers_list = numbers.split(',') 

你可以只得到甚至从列表的数字是:

even_locations_list = numbers_list[1::2] #starting from position 1 (the second object) and jumping 2 steps at a time) 
0

默认实现拆分分裂在任何空格(空格,制表符,你有什么)。

with open('infile.txt','r') as infile, open('outfile.txt','w') as outfile: 
    for line in infile: 
     name, group, op, elements = line.split() 
     elements = [int(i) for i in elements.split(',')[1::2]] 
     for idx, val in enumerate(elements): 
      if op == '-': 
       col4, col5 = val - 1, val 
      else: 
       col4, col5 = val, val + 1 
      output = "\t".join(map(str, 
         ["{}.{}".format(name, idx+1), group, op, col4, col5])) 
      outfile.write(output + "\n") 
+0

我不能得到它的工作,但我结束了上面的东西。我不知道如何使名字去car.1 car.2 plane.1 plane.2等。你能给我建议基于我的编辑? – user3557715