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
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 1,061 Jun-08-2024, 05:16 PM
Last Post: tester_V
  Problem Converting Tradingview Indicator to Python code kralxs 1 2,302 Apr-27-2024, 06:10 PM
Last Post: kralxs
  Convert string to float problem vasik006 8 5,281 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 3,249 May-13-2022, 03:44 PM
Last Post: deanhystad
  Need help converting string to int dedesssse 7 4,034 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  f string concatenation problem growSeb 3 3,044 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 3,653 Jun-15-2021, 03:59 PM
Last Post: Falassion
  How to deal with self and __init__ in function MrFloyd_telomerase 1 2,252 Apr-26-2021, 08:15 PM
Last Post: buran
  Beautify dictionary without converting to string. sharoon 6 4,582 Apr-11-2021, 08:32 AM
Last Post: buran
  string problem Mathisdlg 6 4,009 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg

Forum Jump:

User Panel Messages

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