Python Forum
Isolate a word from a long string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Isolate a word from a long string
#1
Hello everyone,

I am trying to isolate specific words out of a string if present but until now with no success.
If in the string I have 'irg', 'swopt', 'fvol' or 'swol' then I want to write the word found in a specific column. The string I look at have specific caracters.
Example: http:\dsddf;dfdfdf-dfdfd.irg/fdfdfd

I have tried the following code but with no success as it could found none of the word listed above and therefore as a result I have 'Category' column empty.

category = r"irg|swopt|fvol|swvol"
df_susanoo.insert(22, 'Category', df_susanoo['delivery_detail'].str.findall(category).apply(lambda x: x if len(x) >1 else ['not found']).str[-1])
['delivery_detail'] column contains the string to look at.

Would you see what might be wrong?

Thank you in advance for your help Smile ,
Nicolas
Reply
#2
Well that is an impressive line of code you are showing us. But what does it mean? We could not tell as we don't know how df_susanoo is defined and what the contents are. And does the program give you an error message? I bet it does. Show us the complete error message.

You should build your line of code step by step. Start with:
import re
example_data = "http:\dsddf;dfdfdf-dfdfd.irg/fdfdfd"
category = r"irg|swopt|fvol|swvol"
re.findall(category, example_data)
Output:
['irg']
Is this what you wanted? Then continue to add statements until you get what you want.
Let us know the result of your investigation.

If you don't succeed then make a small sample of your program that we can run and shows exactly what goes wrong. Don't forget to include the full error message if you get one.
BashBedlam and nicocorico like this post
Reply
#3
Thank you for your reply! I tried and it works perfectly! Smile


category = r"irg|swopt|fvol|swvol"

for i in tqdm(range(0,len(df_susanoo))):
    if len(re.findall(category, df_susanoo.iloc[i,1])) != 0:
        df_susanoo.iloc[i,22] = re.findall(category, df_susanoo.iloc[i,1])[0]
    else: df_susanoo.iloc[i,22] = ""
df_susanoo.iloc[i,1] is corresponding to the column where there is the string to search on.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 499 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 550 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Isolate all images from a pdf document cybertooth 7 866 Oct-08-2023, 08:55 AM
Last Post: DPaul
  change string in MS word Mr_Blue 8 3,352 Sep-19-2021, 02:13 PM
Last Post: snippsat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,518 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 16,027 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  filter just with the string word jacklee26 2 2,415 Feb-03-2020, 03:25 PM
Last Post: snippsat
  Reverse the string word sneha 2 2,644 Dec-12-2019, 03:37 AM
Last Post: sneha
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,801 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 7,135 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

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