TypeError:“ _ io.TextIOWrapper”对象不可调用:在文件上调用write()时发生

尽管有很多关于此错误的帖子,但我找不到与我的情况相同的帖子。我的代码如下所示:

with open(args.p1) as pop1,open(args.p2) as pop2,open(args.m) as map,\
open('pop1.phgeno','w') as pop1out,open('pop2.phgeno','w') as pop2out,open('snp.txt','w') as snpout:
# ignore header line in pop1 and pop2
pop1.readline()
pop2.readline()

pop1_line,pop2_line,map_line = pop1.readline(),pop2.readline(),map.readline()

while pop1_line and pop2_line and map_line:
    chrom,snpid,gen_dist,phy_loc = map_line.strip().split('\t')
    pop1_info,pop2_info = pop1_line.strip().split(' '),pop2_line.strip().split(' ')
    assert pop1_info[0] == pop2_info[0] and pop1_info[1] == pop2_info[1]

    pop1_snp,pop2_snp = pop1_info[2:],pop2_info[2:]
    #print(pop1_snp)
    #print(pop2_snp)
    allele_set = list(set(pop1_snp + pop2_snp))

    # only biallelic position is retained
    if (len(allele_set)) != 2:
        pop1_line,map.readline()
        continue

    allele1,allele2 = allele_set[0],allele_set[1]
    snpout.write(f'{snpid}\t{chrom}\t{gen_dist}\t{phy_loc}\t{allele1}\t{allele2}\n')
    pop1out.write(''.join(map(lambda x: 1 if x == allele1 else 0,pop1_snp)))
    pop1out.write('\n')
    pop2out.write(''.join(map(lambda x: 1 if x == allele1 else 0,pop2_snp)))
    pop2out.write('\n')

    pop1_line,map.readline()

我遇到错误

  

回溯(最近一次通话最后一次):文件“ tosnP.py”,第45行,在          pop1out.write(''。join(map(lambda x:1如果x == allele1 else 0,pop1_snp))))TypeError:'_io.TextIOWrapper'对象不可调用

我认为在许多帖子中人们都在使用filename(),但是我在这里使用filename.write()。经过一些调试后,我发现问题出在map(lambda x:1如果x == allele1 else 0,pop1_snp)部分。不知道为什么会这样。谢谢!

a409788679 回答:TypeError:“ _ io.TextIOWrapper”对象不可调用:在文件上调用write()时发生

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3119476.html

大家都在问