mad*_*tty 56 python string typeerror
此代码返回以下错误消息:
open(infile,mode ='r',buffering = -1)为in_f,open(outfile,mode ='w',buffering = -1)为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')
Run Code Online (Sandbox Code Playgroud)请有人帮助我.
Gar*_*ees 65
你试图打开每个文件两次!首先你做:
infile=open('110331_HS1A_1_rtTA.result','r')
Run Code Online (Sandbox Code Playgroud)
然后再次传递infile(这是一个文件对象)到open函数:
with open (infile, mode='r', buffering=-1)
Run Code Online (Sandbox Code Playgroud)
open 当然希望它的第一个参数是文件名,而不是打开的文件!
只打开一次文件,你应该没问题.
对于不太具体的情况(不仅仅是问题中的代码 - 因为这是Google针对此一般性错误消息的第一个结果之一.运行某些带有None参数的os命令时也会发生此错误.
例如:
os.path.exists(arg)
os.stat(arg)
Run Code Online (Sandbox Code Playgroud)
当arg为None时,将引发此异常.
您正在尝试将文件对象作为文件名传递.尝试使用
infile = '110331_HS1A_1_rtTA.result'
outfile = '2.txt'
Run Code Online (Sandbox Code Playgroud)
在代码的顶部.
(不仅的一倍,使用open()原因,试图再次打开该文件的问题,这也意味着infile和outfile执行过程中是从来不关,但一旦程序结束,他们可能会得到关闭.)
| 归档时间: |
|
| 查看次数: |
144455 次 |
| 最近记录: |