此代码返回以下错误消息:
open (infile, mode='r', buffering=-1) as in_f, open (outfile, mode='w', buffering=-1) as out_f: TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到文件
# Opens each file to read/modify infile=open('110331_HS1A_1_rtTA.result','r') outfile=open('2.txt','w') import re with open (infile, mode='r', buffering=-1) as in_f, open (outfile, mode='w', buffering=-1) as out_f: f = (i for i in in_f if i.rstrip()) for line in f: _, k = line.split('\t',1) x = re.findall(r'^1..100\t([+-])chr(\d+):(\d+)\.\.(\d+).+$',k) if not x: continue out_f.write(' '.join(x[0]) + '\n')
请有人帮助我。
最佳答案
您尝试打开每个文件两次!首先你要做:
infile=open('110331_HS1A_1_rtTA.result','r')
然后你将 infile
(这是一个文件对象)再次传递给 open
函数:
with open (infile, mode='r', buffering=-1)
open
当然希望它的第一个参数是文件名,而不是打开的文件!
只打开一次文件就可以了。
关于python - TypeError:强制转换为 Unicode:需要字符串或缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6680066/