int()的文本无效,以10为基数的“0 | 0”错误?

2024-10-03 19:32:09 发布

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

我得到一个错误:以10为基数的int()的文本无效'0 | 0'。我不确定问题是格式0 | 0而不是0:0,因为它看起来像是用“:”分隔字段,还是与自我深度位置. 回溯显示:

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'

一段代码看起来像:

^{pr2}$

输入如下:

#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     

Tags: inpy文本most格式错误linefile
3条回答

你这里有几个问题。首先,对于那些花了大量时间编写特定Python风格标准的人来说,代码的格式设置使他们很难阅读。我建议你从PEP8开始,然后从那里发展你的风格。在

我认为这个代码重新格式化保留了代码的含义。如果我错了,请纠正我。在

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: invalid literal for int() with base 10: '0|0'-这是因为调用
时有以下值 gtype = gtypestr.split(':')使用示例输入:

^{pr2}$

str.split(delimiter)的输出总是一个列表,因此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

cutoff没有在您给我们的代码示例中定义,但是我假设0 >= 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')

这就是当你调用gtype[0][2]时给你的IndexError: string index out of range-这相当于调用'0'[2],并且在一个字符串中没有第三个字符。在

这个块看起来像是在尝试处理.split(':')调用返回的第一个子字符串中间的分隔符。在

所以我不知道如何前进:

  • 修复那些gtype[0]子字符串调用以针对不同的字符串工作?在
  • 保留.split(':')并更改截止测试?在
  • 这两种情况都是需要简化的复杂输入文件格式的症状?在

你能告诉我们更多关于输入格式的信息吗?在

需要使用字符串调用eval

int(eval("0|0"))

结果:1

异常意味着字符串'0|0'是传递给int()函数的无效值。要解决此问题,请确保字符串包含有效的integer literal。在

相关问题 更多 >