Python Forum
ValueError: substring not found
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: substring not found
#2
string.index raises a ValueError exception when the substring is not found. You can look for another function that returns a status instead of throwing an exception, but you should start getting used to using exceptions because they are used a lot in Python.

def on_off_finder(in_string):
    #takes a string, looks for On or Off, return true if On
    try:
        start = in_string.index(" ") + 1
        end = in_string.index("ch=") - 1
        if in_string[start:end] == "On":
            return True
        elif in_string[start:end] == "Off":
            return False
    except ValueError:
        pass  # Optionally do something else here
    return False
I'm not exactly sure what this function is supposed to do other that what the comment describe. As it was written it looks like ever in_string is expected to contain "On" or "Off" and that this key will follow the first space and is immediately followed by "ch=". That is really restrictive syntax. Your code will throw a ValueError exception if the string doesn't include a space, or if it doesn't include "ch=" (exactly, including case).

Your program will work better if you don't assume every string has to follow a particular pattern. I would write your function to try to process the string, determine if it is valid, if valid return the result, and if invalid print a message. Look a the invalid strings to help you determine if your parser is wrong or if the file contains strings with a different pattern that you can ignore or process differently

Python has really good tools for parsing strings. I think you might find regex interesting
Reply


Messages In This Thread
ValueError: substring not found - by nby2001 - Mar-08-2020, 06:10 PM
RE: ValueError: substring not found - by deanhystad - Mar-08-2020, 09:00 PM
RE: ValueError: substring not found - by malcomjarr - Aug-08-2022, 05:04 AM
RE: ValueError: substring not found - by DeaD_EyE - Aug-08-2022, 10:05 AM
RE: ValueError: substring not found - by rob101 - Aug-08-2022, 11:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 584 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,123 Apr-08-2023, 06:36 PM
Last Post: Winfried
  Match substring using regex Pavel_47 6 1,488 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,504 Jun-16-2022, 06:59 PM
Last Post: saoko
  Substring Counting shelbyahn 4 6,186 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Python Substring muzikman 4 2,358 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,582 Nov-23-2020, 05:15 PM
Last Post: cananb
  Removing items from list if containing a substring pythonnewbie138 2 2,236 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,373 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,631 Sep-23-2019, 05:34 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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