V
V
VitaliySm2015-02-12 23:17:02
Python
VitaliySm, 2015-02-12 23:17:02

How to remove part of a string using a regular expression?

There is a spider that finds a lot of lines of this kind:
Code:
____code_____ I want to remove "Code:" from these lines, i.e. so that only one ___ code is transmitted ___
I tried to do this:

def parse_code(x):
    s = re.sub('(^[\W]{3}\:{1}\s{1})', '', x)
    if not s:
        return None
    return s

so:
def parse_code(x):
    s = str(x).translate(None, 'Код: ')
    if not s:
        return None
    return s

All this is divided for transfer to the item:
class TestItem(Item):
code = Field(input_processor=MapCompose(parse_code))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceJOKER, 2015-02-12
@VitaliySm

If only "Code:", then what for regular season?
Isn't there a function of something like - var.replace('Код:', '');
and yes, of course there is - www.tutorialspoint.com/python/string_replace.htm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question