ValueError: 在较大代码中出现无效的int()字面量错误,而非单独运行时
defaultdict(<type 'int'>, {'match': 1})
[(0, 0)]
[]
[]
[]
Traceback (most recent call last):
File "Joinomattic_1.py", line 409, in <module>
verboseprint (magic(matching))
File "Joinomattic_1.py", line 408, in <lambda>
magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp.
ValueError: invalid literal for int() with base 10: '''
我下面的代码总是出现上面的错误,但只有在它作为一个更大程序的一部分运行时才会出现这个错误,单独运行时没有错误,输出也能正常写入到输出文件中。这个错误是什么意思,为什么它只在与其他代码一起运行时才会出现?下面是我作为更大程序一部分时的代码:
n = 0
consec_matches = []
chars = defaultdict(int)
for k, group in groupby(zip(line1_u_i, line2_u_rev_comp_i), class_chars):
elems = len(list(group))
chars[k] += elems
if k == 'match':
consec_matches.append((n, n+elems-1))
n += elems
verboseprint (chars)
verboseprint (consec_matches)
verboseprint ([x for x in consec_matches if x[1]-x[0] >= 9])
list = [x for x in consec_matches if x[1]-x[0] >= 9]
flatten_list= [x for y in list for x in y]
verboseprint (flatten_list)
matching=[y[1] for y in list for x in y if x ==0 ]
verboseprint (matching)
magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp.
verboseprint (magic(matching))
line2_u_rev_comp_i_l = line2_u_rev_comp_i[magic(matching):]
line3=line1_u_i+line2_u_rev_comp_i_l
verboseprint (line3)
if line3:
verboseprint(line3, file = output_file)
这是它单独运行时的代码:
from __future__ import print_function
from collections import defaultdict
from itertools import groupby
import argparse #imports the argparse module so it can be used
from itertools import izip
parser = argparse.ArgumentParser() #simplifys the wording of using argparse as stated in the python tutorial
parser.add_argument("-r1", type=str, action='store', dest='input1', help="input the forward read file") # allows input of the forward read
parser.add_argument("-r2", type=str, action='store', dest='input2', help="input the reverse read file") # allows input of the reverse read
parser.add_argument("-v", "--verbose", action="store_true", help=" Increases the output, only needs to be used to provide feedback to Tom for debugging")
parser.add_argument("-n", action="count", default=0, help="Allows for up to 5 mismatches, however this will reduce accuracy of matching and cause mismatches. Default is 0")
parser.add_argument("-fastq", action="store_true", help=" States your input as fastq format")
parser.add_argument("-fasta", action="store_true", help=" States your input as fasta format")
parser.add_argument("-o", "--output", help="Directs the output to a name of your choice")
args = parser.parse_args()
output = str(args.output)
def class_chars(chrs):
if 'N' in chrs:
return 'unknown'
elif chrs[0] == chrs[1]:
return 'match'
else:
return 'not_match'
#with open(args.output, 'w') as output_file:
output_file= open(output, "w")
s1 = 'aaaaaaaaaaN123bbbbbbbbbbQccc'
s2 = 'aaaaaaaaaaN456bbbbbbbbbbPccc'
n = 0
consec_matches = []
chars = defaultdict(int)
for k, group in groupby(zip(s1, s2), class_chars):
elems = len(list(group))
chars[k] += elems
if k == 'match':
consec_matches.append((n, n+elems-1))
n += elems
print (chars)
print (consec_matches)
print ([x for x in consec_matches if x[1]-x[0] >= 9])
list = [x for x in consec_matches if x[1]-x[0] >= 9]
flatten_list= [x for y in list for x in y]
print (flatten_list)
matching=[y[1] for y in list for x in y if x ==0 ]
print (matching)
magic = lambda matching: int(''.join(str(i) for i in matching)) # Generator exp.
print (magic(matching))
s2_l = s2[magic(matching):]
line3=s1+s2_l
print (line3)
if line3:
print(line3, file = output_file)
单独运行时的输出是:
defaultdict(<type 'int'>, {'not_match': 4, 'unknown': 1, 'match': 23})
[(0, 9), (14, 23), (25, 27)]
[(0, 9), (14, 23)]
[0, 9, 14, 23]
[9]
9
aaaaaaaaaaN123bbbbbbbbbbQcccaN456bbbbbbbbbbPccc
1 个回答
0
从你的输出结果来看,matching
是一个空列表。所以在使用 join
之后,你得到的是一个空字符串 ''
,这个空字符串是无法转换成整数的。你可以试试:
lambda matching: int(''.join(str(i) for i in matching) or 0)
如果 matching == []
,那么这个会返回 0。