注册 登录
编程论坛 Python论坛

python新手最后一步不会写,请教前辈们

liyibo0719 发布于 2019-08-24 04:47, 1780 次点击
import re

gencode="gencode.v26.annotation.txt"
file="wt.txt"

a=[]
trInfo={}

for line in open(gencode, 'r').readlines():
    if line is None:
        break
    if re.findall('ch.*',line):
        tmp=line.replace('"','')
        tmp=tmp.replace(';','')
        tmp=tmp.rstrip()
        a.append(tmp.split())
#trInfo字典key是id,value是type
for x in a:
    if len(x) >17:
        id=x[11]
        type=x[17]
        if id not in trInfo:
            trInfo[id]={type}
        else:
            continue

b=[]
length={}
for line in open(file, 'r').readlines():
    if line is None:
        break
    tmp=line.rstrip()
    b.append(tmp.split())
for i in b:
    #如果i[2]在字典的key里,就把i[9]的长度当作value加入到trInfo字典里
    if i[2] in trInfo.keys():
        length[i[2]]=len(i[9])
        trInfo[i[2]]={length[i[2]]}

#打印出id,和它对应的type和长度
with open ("output.txt") as f1:
    f1.write("{}\t{}\t{}\n".format()


请教前辈:蓝色字体标注是不知道对不对,最后红色标注的输出是不知道怎么写。
要做的就是两个文件中分别提取两列信息,其中一列他们共有(不过顺序不同,有的还有重复),将这三列对应起来组成一个文件即可
1 回复
#2
liyibo07192019-08-27 03:16
问题自己解决啦。
for i in b:
    if i[2] in trInfo:
        with open ("output.txt",'a+') as f1:
           f1.write("{}\t{}\t{}\n".format(i[2],len(i[9]),trInfo[i[2]]))
1