-5

For example here I have a line '2020-08-12 13:45:04 0.86393 %contrastWeber' and I need to extract 9 characters before ' %contrastWeber' string in text file. So, here my answer will be '0.86393'. I don't understand how to do it in Python? please help

1

4
  • 2
    Please do not post images of text. Commented Aug 20, 2020 at 3:51
  • Why not just use a simple regex search: "(.{9})%contrastWeber" Commented Aug 20, 2020 at 3:52
  • Welcome! Give this a read for detail on asking questions. For more help here, I suggest you update your answer with text output of the above image, things you've tried, researched, and ultimately, a little more detail. Commented Aug 20, 2020 at 3:57
  • Convert the string to list using str.split(" ") and then find index of %contrastWeber in list. Then the text you want shall be at index 1 less than previous index you found. Commented Aug 20, 2020 at 4:12

1 Answer 1

0
line = '2020-08-12 13:45:04 0.86393 %contrastWeber'

position = line.index('%contrastWeber')

if position >= 0:
    starting = position - 9
    if starting < 0:
        starting = 0
    ending = position
    print(line[starting : ending])
else:
    print('is not found')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.