Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Testing Split elements
#1
Greetings!
I'm looking for a line that has multiple spaces and the word "Motherboard:" at the beginning of the line.
If it found I would split the line and get the fourth element, which is a Serial number.
I'm trying to test splitted elements and if an element [4]==0 write something else instead.
I was sure it will work but it does not. I got this error "IndexError: list index out of range"

if '    Motherboard:' in rn_l :
	mb=re.split(r" {2,}", rn_l)
	if not mb[4] :
	   mb[4] ="No Serial Num"
	else :
		print (f" Motherboard Serial number   -->> {mb[4]}")      
Reply
#2
Line 2 will split and give you back a list. But you are not guaranteed for that list to be any particular length. If the list has less than 5 elements, then referring to mb[4] (as done on line 3) will be an error.
Reply
#3
what about
line = line.strip()
if line.strip().startswith('Motherboard:'):
    try:
        print (f"Motherboard Serial number   -->> {line.split()[4]}")
    except IndexError:
       print("No Serial Num")
perfringo and snippsat like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I do not have a problem splitting.
I need to replace the fourth element in a split if the element does not exist.
Here is a line to split:
   Motherboard:   Ao11   Build-211   DT 2021 SN-0123A456
if the line has nothing after the word "Motherboard:"
   Motherboard:  
I want to print something like "not there" or "empty" or "No Serial Num"
I thought I could split the line and test elements if they are ==0 or not.
That is why I'm testing element 4 if it is empty.

    mb=re.split(r" {2,}", rn_l)
    if not mb[4] :
       mb[4] ="No Serial Num"
    else :
        print (f" Motherboard Serial number   -->> {mb[4]}")
Reply
#5
(May-08-2021, 09:56 PM)tester_V Wrote: That is why I'm testing element 4 if it is empty.
Yes,but you can not do like this as mb[4] will give IndexError when empty.
Then you need to do as buran show,so for you code it would be like this.
import re

rn_l = 'Motherboard:   Ao11   Build-211   DT 2021 SN-0123A456'
#rn_l = 'Motherboard:'
mb = re.split(r" {2,}", rn_l)
try:
    print (f"Motherboard Serial number -->> {mb[3]}")
except IndexError:
    print("No Serial Num")
Output:
Motherboard Serial number -->> DT 2021 SN-0123A456 # Test empty No Serial Num
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,114 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,585 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  How to sum up the elements of an integer using python split/join? mohanraj1986 5 3,498 Aug-27-2018, 09:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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