Python Forum
how to deal with problem of converting string to int - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to deal with problem of converting string to int (/thread-31825.html)



how to deal with problem of converting string to int - usthbstar - Jan-05-2021

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.


RE: how to deal with problem of converting string to int - perfringo - Jan-05-2021

I suspect that you want to convert value at index 0 and not at index 1.