Home > Mobile >  How can I remove this specific section of a string without also removing the 'm' in the li
How can I remove this specific section of a string without also removing the 'm' in the li

Time:12-01

This is my code now:

def extract_categories(line: str):
    new_line = re.sub('[    $   ]', '', line)
    newer_line
    print(new_line)

I want it to print this

['action', 'comedy', 'crime', 'drama', 'thriller']

but it prints this:

m448hrs.19826.9022289['action','comedy','crime','drama','thriller']

This is the input I am using:

"m4    $    48 hrs.    $    1982    $    6.90    $    22289    $    ['action', 'comedy', 'crime', 'drama', 'thriller']"

I need the first part removed, I tried removing exactly 'm448hrs.19826.9022289' but then also the m's from 'crime' and 'drama' disappeared, what should I do here? Im new to python so any help would be appreciated.

CodePudding user response:

You can try breaking down the string based on pattern later you replace un wanted items as

st = "m4    $    48 hrs.    $    1982    $    6.90    $    22289    $    ['action', 'comedy', 'crime', 'drama', 'thriller']"
new = []
mask = st.split('    $    ')[-1][1:-1].replace("'","").replace(" ","")
new.append(mask.split(','))
print(new[0])

Gives #

['action', 'comedy', 'crime', 'drama', 'thriller']

CodePudding user response:

You can use index() or find() to find index of substring you want to print and slice the string from substring index to end of the string. index() will raise ValueError if the substring is not found, and find() will return -1.

line = "m4    $    48 hrs.    $    1982    $    6.90    $    22289    $    ['action', 'comedy', 'crime', 'drama', 'thriller']"
line = line[line.index('['):]
print(line)

To avoid exception, use find() with conditional statement in your function:

line = "m4    $    48 hrs.    $    1982    $    6.90    $    22289    $    ['action', 'comedy', 'crime', 'drama', 'thriller']"

def extract_categories(line: str):
    return line[found:] if (found := line.find('[')) != -1 else 'Not found'

print(extract_categories(line))

Or:

line = "m4    $    48 hrs.    $    1982    $    6.90    $    22289    $    ['action', 'comedy', 'crime', 'drama', 'thriller']"

def extract_categories(line: str):
    return ('Not Found',line[(found:=line.find('[')):])[found!=-1]

print(extract_categories(line))
  • Related