Python Forum
How to extract data between two strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract data between two strings
#2
you can use Regular Expressions (regex), but I'm not as skilled as I would like to be.

Alternatively you can use the following
line = "Group1   ABC_YJK_02_S_2019-08-01    2"
beginning, end = line.find('_'), line.find('_S_')
result = line[beginning+1 : end]
print(result)
or

table = [
"Group1   ABC_YJK_02_S_2019-08-01    2",
"         ABC_YMK_5_S_2019-08-01     5",
"         ABC_JKL_S_2019-08-04       10",
"Group2   BCA_POL_S_2019-08-01       4",
"         BCA_PAL_S_2019-08-01       5",
"         BCA_TYP_S_2019-08-01       50",
"         BCA_DIST_S_2019-08-01      23",
"         BCA_STA_S_2019-08-01       3"]

n = len(table)

resultsTable = []

for i in range(n):
    beginning, end = table[i].find('_'), table[i].find('_S_')
    result = table[i][beginning+1 : end]
    resultsTable.append(result)
Paul
Reply


Messages In This Thread
RE: How to extract data between two strings - by paul18fr - Aug-08-2019, 08:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Training a model to identify specific SMS types and extract relevant data? lord_of_cinder 0 978 Oct-10-2022, 04:35 AM
Last Post: lord_of_cinder
  extract and plot data from a txt file usercat123 2 1,230 Apr-20-2022, 06:50 PM
Last Post: usercat123
  How to extract data from paragraph using Machine Learning with python? bccsthilina 2 3,044 Jul-27-2020, 07:02 AM
Last Post: hussainmujtaba
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 2,159 Jun-09-2020, 06:05 AM
Last Post: Pan
  how to extract financial data from photocopy of document angela1 6 3,680 Feb-15-2020, 05:50 PM
Last Post: jim2007
  How to extract different data groups from multiple CSV files using python Rafiz 3 3,242 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  Extract data between two dates from a .csv file using Python 2.7 sujai_banerji 1 10,368 Nov-15-2017, 09:48 PM
Last Post: snippsat
  I'm working onn below code to extract data from excel using python kiran 1 3,279 Oct-24-2017, 01:42 PM
Last Post: kiran

Forum Jump:

User Panel Messages

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