TypeError:“\u io.TextIOWrapper”对象不可调用:在fi上调用write()时发生

2024-06-16 10:17:20 发布

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

虽然有很多关于这个错误的帖子,但是我找不到一个和我的情况一样的。我的代码如下所示:

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, pop2_line, map_line = pop1.readline(), pop2.readline(), 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, pop2_line, map_line = pop1.readline(), pop2.readline(), map.readline()

我搞错了

Traceback (most recent call last): File "toSNP.py", line 45, in pop1out.write(''.join(map(lambda x: 1 if x == allele1 else 0, pop1_snp))) TypeError: '_io.TextIOWrapper' object is not callable

我认为在很多帖子中人们都在使用filename(),而我在这里使用filename.write()。经过一些调试,我发现问题出现在map(lambda x:1 if x==allele1 else 0,pop1\u snp)部分。不知道为什么会这样。谢谢


Tags: andinfomapreadlineifaslineopen