Python Forum

Full Version: how to deal with problem of converting string to int
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using the following function:

def load_target_class(input_dir):
  """Loads target classes."""
  with tf.gfile.Open(os.path.join(input_dir, 'target_class.csv')) as f:
    return {row[0]: int(row[1]) for row in csv.reader(f) if len(row) >= 2}
For example, target_class.csv contains the following two colomns:

1,"tench, Tinca tinca"
2,"goldfish, Carassius auratus"
3,"great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias"
4,"tiger shark, Galeocerdo cuvieri"
5,"hammerhead, hammerhead shark"
6,"electric ray, crampfish, numbfish, torpedo"
7,stingray
8,cock
The output error is:

ValueError: invalid literal for int() with base 10: 'tench, Tinca tinca'
How can I deal with this error?
more specifically, the error is in int(row[1]) i,e problem of converting string to int.
I suspect that you want to convert value at index 0 and not at index 1.