2014-04-21 186 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