ValueError: invalid literal for int() with base 10: '' error occurs when part of larger code, not when alone [closed]

StackOverflow https://stackoverflow.com/questions/23569062

  •  19-07-2023
  •  | 
  •  

سؤال

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: '''

The error above keeps on occurring for my code below, however it only occurs when part of a larger program, when run by itself no error occurs and the output is written to the output_file. What does this error mean and why does it occur only with other code? Below is my code when part of a larger program:

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)

And here it is stand alone :

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)

When stand alone the output is:

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
هل كانت مفيدة؟

المحلول

As your output shows you, matching is an empty list. After the join, therefore, you get an empty string '', which cannot be converted to an integer. Try:

lambda matching: int(''.join(str(i) for i in matching) or 0)

This will return 0 if matching == [].

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top