Python Forum
how to deal with problem of converting string to int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to deal with problem of converting string to int
#1
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.
Reply
#2
I suspect that you want to convert value at index 0 and not at index 1.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help converting string to int dedesssse 7 575 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  f string concatenation problem growSeb 3 499 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 620 Jun-15-2021, 03:59 PM
Last Post: Falassion
  How to deal with self and __init__ in function MrFloyd_telomerase 1 463 Apr-26-2021, 08:15 PM
Last Post: buran
  Beautify dictionary without converting to string. sharoon 6 947 Apr-11-2021, 08:32 AM
Last Post: buran
  string problem Mathisdlg 6 1,267 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Converting string to hex triplet menator01 4 1,440 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 1,049 Mar-31-2020, 10:36 AM
Last Post: buran
  Converting query string as a condition for filter data. shah_entrance 1 740 Jan-14-2020, 09:22 AM
Last Post: perfringo
  Unicode string index problem luoheng 6 1,230 Nov-23-2019, 03:04 PM
Last Post: luoheng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020