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
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,779 May-13-2022, 03:44 PM
Last Post: deanhystad
  Need help converting string to int dedesssse 7 2,615 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  f string concatenation problem growSeb 3 2,212 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,616 Jun-15-2021, 03:59 PM
Last Post: Falassion
  How to deal with self and __init__ in function MrFloyd_telomerase 1 1,708 Apr-26-2021, 08:15 PM
Last Post: buran
  Beautify dictionary without converting to string. sharoon 6 3,297 Apr-11-2021, 08:32 AM
Last Post: buran
  string problem Mathisdlg 6 2,783 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Converting string to hex triplet menator01 4 4,218 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 2,553 Mar-31-2020, 10:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

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