Skip to main content
Added explicit exception
Source Link

I would go with @Randyr answer but I would change the read and convert method as follows for two reasons; 1) the output method requires storing all your answers when they wont be processed again and 2) good python apologises rather then asks for permission.

def read_and_convert_file(args):
    """Read the given input file and convert each hexadecimal value
    to decimal value and output to file
    """
    line_num = 0
    out_file = args['input_file'] + ".2decimal"
    with open(out_file, 'a+') as output:
        with open(args['input_file']) as input_file:
            for line in input_file:
                line_num += 1
                try: 
                    converted = str(int(line, 0))
                    output.write(converted + "\n")
                    print('Converted: {}'.format(converted))
                except ValueError:
                    print('Invalid text on line',line_num)
                    

I would go with @Randyr answer but I would change the read and convert method as follows for two reasons; 1) the output method requires storing all your answers when they wont be processed again and 2) good python apologises rather then asks for permission.

def read_and_convert_file(args):
    """Read the given input file and convert each hexadecimal value
    to decimal value and output to file
    """
    line_num = 0
    out_file = args['input_file'] + ".2decimal"
    with open(out_file, 'a+') as output:
        with open(args['input_file']) as input_file:
            for line in input_file:
                line_num += 1
                try: 
                    converted = str(int(line, 0))
                    output.write(converted + "\n")
                    print('Converted: {}'.format(converted))
                except:
                    print('Invalid text on line',line_num)
                    

I would go with @Randyr answer but I would change the read and convert method as follows for two reasons; 1) the output method requires storing all your answers when they wont be processed again and 2) good python apologises rather then asks for permission.

def read_and_convert_file(args):
    """Read the given input file and convert each hexadecimal value
    to decimal value and output to file
    """
    line_num = 0
    out_file = args['input_file'] + ".2decimal"
    with open(out_file, 'a+') as output:
        with open(args['input_file']) as input_file:
            for line in input_file:
                line_num += 1
                try: 
                    converted = str(int(line, 0))
                    output.write(converted + "\n")
                    print('Converted: {}'.format(converted))
                except ValueError:
                    print('Invalid text on line',line_num)
                    
Source Link

I would go with @Randyr answer but I would change the read and convert method as follows for two reasons; 1) the output method requires storing all your answers when they wont be processed again and 2) good python apologises rather then asks for permission.

def read_and_convert_file(args):
    """Read the given input file and convert each hexadecimal value
    to decimal value and output to file
    """
    line_num = 0
    out_file = args['input_file'] + ".2decimal"
    with open(out_file, 'a+') as output:
        with open(args['input_file']) as input_file:
            for line in input_file:
                line_num += 1
                try: 
                    converted = str(int(line, 0))
                    output.write(converted + "\n")
                    print('Converted: {}'.format(converted))
                except:
                    print('Invalid text on line',line_num)