Python int()的文本无效,基数为10“0 | 0”的错误?
我得到的错误是:基数为10“0 | 0”的int的文本无效。我不确定问题是格式0 | 0而不是0:0,因为它看起来像是将字段拆分为:,还是与self.depthpos有关。追踪显示:Python int()的文本无效,基数为10“0 | 0”的错误?,python,Python,我得到的错误是:基数为10“0 | 0”的int的文本无效。我不确定问题是格式0 | 0而不是0:0,因为它看起来像是将字段拆分为:,还是与self.depthpos有关。追踪显示: Traceback (most recent call last): File "vcftopf_p1_commandline.py", line 200, in <module> snps[i]=snp(lin) File "vcftopf_p1_commandline.py", line 1
Traceback (most recent call last):
File "vcftopf_p1_commandline.py", line 200, in <module>
snps[i]=snp(lin)
File "vcftopf_p1_commandline.py", line 140, in __init__
if int(gtype[self.depthpos])>=cutoff:
ValueError: invalid literal for int() with base 10: '0|0'
输入看起来像:
self.info=lsp[7]
isplit=self.info.split(';')
infonum=0
for infofield in isplit:
if infofield.split('=')[0]=='AA':
self.ancestinfo=True
self.ancest=infofield.split('=')[1]
if self.ancest=='.':
self.failed=1
return
infonum+=1
self.format=lsp[8]
self.gtypes=lsp[9:]
fsp=self.format.split(':')
self.fdpos=-1
self.depthpos=-1
formatnum=0
for field in fsp:
if field=='DP':
self.depthpos=formatnum
formatnum+=1
#if (len(self.gtypes)!=nsamp):
# raise(" incorrect number of individuals in line "+line)
i=0
for fd in fsp:
if fd=="FD":
self.fdpos=i
i=i+1
if self.fdpos!=-1:
for gtypestr in self.gtypes:
gtype=gtypestr.split(':')
if gtype[self.fdpos]=='0':
self.nalt+=int( gtype[0][0]=='1')+int( gtype[0][2]=='1')
self.nref+=int( gtype[0][0]=='0')+int( gtype[0][2]=='0')
#print self.nref,self.nalt
else:
for gtypestr in self.gtypes:
gtype=gtypestr.split(':')
#print gtype
if gtype[0]!='.' and gtype[0]!='./.':
if int(gtype[self.depthpos])>=cutoff:
self.nalt+=int( gtype[0][0]=='1')+int( gtype[0][2]=='1')
self.nref+=int( gtype[0][0]=='0')+int( gtype[0][2]=='0')
#print self.nref,self.nalt
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT HG01112 HG01113 HG01119
1 14674 . G A 100 PASS . GT 0|0 0|0 0|0
int接受整数,您试图给出一个小数,所以需要使用float
您的代码:
更改并尝试以下操作:
异常意味着字符串“0 | 0”是传递给int函数的无效值。要解决此问题,请确保字符串包含有效的。您需要对字符串使用eval调用
int(eval("0|0"))
结果:1您在这里遇到了几个问题。首先,对于那些花了大量时间在特定Python风格标准中编程的人来说,代码的格式设置很难阅读。我建议你从那里开始,并从那里发展你的风格 我认为此代码重新格式化保留了代码的含义。如果我错了,请纠正我
self.info = lsp[7]
isplit = self.info.split(';')
infonum = 0
for infofield in isplit:
if infofield.split('=')[0] == 'AA':
self.ancestinfo = True
self.ancest = infofield.split('=')[1]
if self.ancest == '.':
self.failed = 1
return
infonum += 1
self.format = lsp[8]
self.gtypes = lsp[9:]
fsp = self.format.split(':')
self.fdpos = -1
self.depthpos = -1
formatnum = 0
for field in fsp:
if field == 'DP':
self.depthpos = formatnum
formatnum += 1
#if len(self.gtypes) != nsamp:
# raise(" incorrect number of individuals in line " + line)
i = 0
for fd in fsp:
if fd == "FD":
self.fdpos = i
i = i + 1
if self.fdpos != -1:
for gtypestr in self.gtypes:
gtype = gtypestr.split(':')
if gtype[self.fdpos] == '0':
self.nalt += int(gtype[0][0] == '1') + int(gtype[0][2] == '1')
self.nref += int(gtype[0][0] == '0') + int(gtype[0][2] == '0')
#print self.nref,self.nalt
else:
for gtypestr in self.gtypes:
gtype = gtypestr.split(':')
#print gtype
if gtype[0] != '.' and gtype[0] != './.':
if int(gtype[self.depthpos]) >= cutoff:
self.nalt += int(gtype[0][0] == '1') + int(gtype[0][2] == '1')
self.nref += int(gtype[0][0] == '0') + int(gtype[0][2] == '0')
#print self.nref,self.nalt
第一个问题
最初的问题是ValueError:基数为10的int的文本无效:“0 | 0”-这是因为在调用
gtype=gtypestr.split':'与您的示例输入:
self.gtypes = ['0|0', '0|0', '0|0']
gtypestr = '0|0'
self.depthpos = -1
str.splitdelimiter的输出总是一个列表,因此gtype的值为['0 | 0']
gtype[self.depthpos]等于gtype[-1],这等于gtype[0],因为gtype是长度为1的列表
因此,您调用int'0 | 0',这将给出错误
第二个问题
将gtypestr.split“:”更改为gtypestr.split“|”的建议假定错误来自于对错误字符的拆分。这种变化给你
gtype = ['0', '0']
int(gtype[self.depthpos]) == int(gtype[-1]) == int('0') == 0
您给我们的代码示例中没有定义截断,但我将假设0>=截断,然后我们运行此块:
self.nalt += int(gtype[0][0] == '1') + int(gtype[0][2] == '1')
self.nref += int(gtype[0][0] == '0') + int(gtype[0][2] == '0')
这就是为什么调用gtype[0][2]时会出现索引器错误:字符串索引超出范围-这相当于调用“0”[2],并且单个字符串中没有第三个字符
这个块看起来好像是在第一个子串中间的一个分隔符中工作,在第一个子串中,由.Sele::'Cult.< /P>返回。 因此,我不确定如何向前迈进:
修复那些gtype[0]子字符串调用,以针对不同的字符串工作? 保留.split“:”并更改截止测试? 这两种都是需要简化的复杂输入文件格式的症状?您能告诉我们有关输入格式的更多信息吗?请提供一个。您是否尝试将gtype=gtypestr.split':'替换为gtype=gtypestr.split'|'?我尝试过,但后来出现了一个错误,称字符串索引超出范围。您的代码很难理解,不清楚您试图实现什么。但是int需要一个字符串,该字符串只包含要转换为单个整数的数字。你传递的字符串有两个数字,中间有一个。请修正你的代码的缩进。然后我得到了无效的文字:浮点:0,你试试替换:gType=gTyrStr.Selp::‘GType=gTyrStr.St```,这是因为gType(0)[2 ]试图得到长度为1的字符串的第三个元素。谢谢。您的评论已经澄清,我现在可以运行脚本了。这是别人的代码,我不懂python,我被指示在我的文件上使用。我意识到我不应该分割0 | 0,应该访问该字段的第一个和第三个元素,忽略|。我正在计算该字段中0和1的数量,因为输入可以采用0 | 0,0 | 1,1 | 0,1 | 1的形式。我认为gtype[self.depthpos]应该调用不同的字符串,正如您所说的。
gtype = ['0', '0']
int(gtype[self.depthpos]) == int(gtype[-1]) == int('0') == 0
self.nalt += int(gtype[0][0] == '1') + int(gtype[0][2] == '1')
self.nref += int(gtype[0][0] == '0') + int(gtype[0][2] == '0')