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

2024-10-04 05:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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)

Tags: transstringtypesys数字start汽车infile
3条回答

您可以使用split方法来执行此操作:

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的默认实现对任何空格(空格、制表符、您拥有的内容)进行拆分。你知道吗

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")

相关问题 更多 >