| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1649 人关注过本帖, 1 人收藏
标题:python新手最后一步不会写,请教前辈们
只看楼主 加入收藏
liyibo0719
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2019-8-6
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:1 
python新手最后一步不会写,请教前辈们
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()


请教前辈:蓝色字体标注是不知道对不对,最后红色标注的输出是不知道怎么写。
要做的就是两个文件中分别提取两列信息,其中一列他们共有(不过顺序不同,有的还有重复),将这三列对应起来组成一个文件即可
搜索更多相关主题的帖子: for line if tmp type 
2019-08-24 04:47
liyibo0719
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2019-8-6
收藏
得分:0 
问题自己解决啦。
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]]))
2019-08-27 03:16
快速回复:python新手最后一步不会写,请教前辈们
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014595 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved