Python 以逗号分隔、制表符分隔的混合格式组织数据

Python 以逗号分隔、制表符分隔的混合格式组织数据,python,file-io,data-structures,Python,File Io,Data Structures,我是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”数字从逗号分隔的数据中去掉,如

我是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,然后将其放到第五列。如果是“-”,则数字在第五列中减去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:所以经过你们的搜索和帮助,我现在有了这样的东西。这给了我正确的输出,但我现在唯一的问题是我很难正确命名它。(汽车1、汽车2、汽车3、飞机1、飞机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)

按制表符拆分每一行;然后分割逗号上的最后一项(数字列表)。这将为您提供处理所需的所有位。

您可以使用拆分方法进行处理:

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)

split的默认实现在任何空白(空格、制表符等)上拆分


我无法让它工作,但我最终得到了类似上面的东西。我不知道如何让名字变成car.1 car.2 plane.1 plane.2等。你能根据我的编辑给我建议吗?
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")