2

Struggling trying to find a way to do this, any help would be great.

I have a long string – it’s the Title field. Here are some samples.

AIR-LAP1142N-A-K
AIR-LP142N-A-K
Used Airo 802.11n Draft 2.0 SingleAccess Point AIR-LP142N-A-9
Airo AIR-AP142N-A-K9 IOS Ver 15.2
MINT Lot of (2) AIR-LA112N-A-K9 - Dual-band-based 802.11a/g/n
Genuine Airo 112N  AP AIR-LP114N-A-K9 PoE
Wireless AP AIR-LP114N-A-9  Airy 50 availiable

I need to pull the part number out of the Title and assign it to a variable named ‘PartNumber’. The part number will always start with the characters ‘AIR-‘.

So for example-

Title = ‘AIR-LAP1142N-A-K9 W/POWER CORD’
PartNumber = yourformula(Title)

Print (PartNumber) will output AIR-LAP1142N-A-K9

I am fairly new to python and would greatly appreciate help. I would like it to ONLY print the part number not all the other text before or after.

1
  • Try looking in to regex. import re Commented Jun 19, 2015 at 21:55

4 Answers 4

3

What you’re looking for is called regular expressions and is implemented in the re module. For instance, you’d need to write something like :

>>> import re
>>> def format_title(title):
...     return re.search("(AIR-\S*)", title).group(1)
>>> Title = "Cisco AIR-LAP1142N-A-K9 W/POWER CORD"
>>> PartNumber = format_title(Title)
>>> print(PartNumber)
AIR-LAP1142N-A-K9

The \S ensures you match everything from AIR- to the next blank character.

Sign up to request clarification or add additional context in comments.

Comments

2
def yourFunction(title):
    for word in title.split():
        if word.startswith('AIR-'):
            return word

>>> PartNumber = yourFunction(Title)
>>> print PartNumber

AIR-LAP1142N-A-K9

Comments

2

This is a sensible time to use a regular expression. It looks like the part number consists of upper-case letters, hyphens, and numbers, so this should work:

import re
def extract_part_number(title):
    return re.search(r'(AIR-[A-Z0-9\-]+)', title).groups()[0]

This will throw an error if it gets a string that doesn't contain something that looks like a part number, so you'll probably want to add some checks to make sure re.search doesn't return None and groups doesn't return an empty tuple.

Comments

0

You may/could use the .split() function. What this does is that it'll split parts of the text separated by spaces into a list.

To do this the way you want it, I'd make a new variable (named whatever); though for this example, let's go with titleSplitList. (Where as this variable is equal to titleSplitList = Title.split())

From here, you know that the part of text you're trying to retrieve is the second item of the titleSplitList, so you could assign it to a new variable by:

PartNumber = titleSplitList[1]

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.