Python Function To Find String Between Two Markers [duplicate] - python

This question already has answers here:
How to extract the substring between two markers?
(22 answers)
Closed 4 years ago.
I'm looking to build a string function to extract the string contents between two markers. It returns an extraction list
def extract(raw_string, start_marker, end_marker):
... function ...
return extraction_list
I know this can be done using regex but is this fast? This will be called billions of times in my process. What is the fastest way to do this?
What happens if the markers are the same and appear and odd number of times?
The function should return multiple strings if the start and end markers appear more than once.

You probably can't go faster than:
def extract(raw_string, start_marker, end_marker):
start = raw_string.index(start_marker) + len(start_marker)
end = raw_string.index(end_marker, start)
return raw_string[start:end]
But if you want to try regex, just try to benchmark it. There's a good timeit module for it.

Related

Split python string in a specific way [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Match text between two strings with regular expression
(3 answers)
Closed 5 months ago.
I have a string like a = 'This is an example string that has a code !3377! this is the code I want to extract'.
How can I extract 3377 from this string, i.e., the part surrounded by !?
There are multiple ways of doing what you are looking for. But the most optimal way of doing it would be by using regular expressions.
For example, in the case you gave:
import re
def subtract_code_from(sentence: str) -> str:
m = re.search(r'\w?!(\d+)!\w?', sentence)
return m.group(0)
Keep in mind that what I've done is a very quick and loose solution I implemented in five minutes. I don't know what other types of particular cases you could encounter for each sentence. So it is your job to implement the proper regex to match all the cases.
I encourage you to follow this tutorial. And you can use this website to build your regexes.
Good luck.

How to use str.startswith with multiple strings? [duplicate]

This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 1 year ago.
I've tried using the or function to input multiple words for the same output, but it only takes the first word as the input and not the rest. How do I solve this? Thanks!
For instance:
message.content.startswith("hi" or "hey")
only takes in "hi" as an input and not "hey".
I've tried adding the words in to a list and it doesn't work as well. I'm relatively new to coding so i'm sorry in advance if it's a stupid question
You can code like this:
message.content.startswith(("hi", "hey"))
From the Python documentation for str.startswith(prefix[, start[, end]]), I've added emphasis:
Return True if string starts with the prefix, otherwise return
False. prefix can also be a tuple of prefixes to look for. With
optional start, test string beginning at that position. With optional
end, stop comparing string at that position.

Generate multiple OR/AND statements [duplicate]

This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 4 years ago.
I am working on python script that splits text in different blocks based on keywords used in text.
Currently I split text into blocks with sth like this (for 1 block, others have pretty much the same strucure):
if (line.strip().lower().startswith('ключевые навыки')
or line.strip().lower().startswith('дополнительная информация')
or line.strip().lower().startswith('знания')
or line.strip().lower().startswith('личные качества')
or line.strip().lower().startswith('профессиональные навыки')
or line.strip().lower().startswith('навыки')):
But, it is possible that list of keywords is going to expand. Is there a possibility to generate multiple or statements based on some array of possible keywords?
Try this code
values=['ключевые навыки','дополнительная информация','знания']
val=True
#enter any words you want to check
while val
for i in values:
if (line.strip().lower().startswith(i)):
#whatever code you want to implement
val=False
#to exit loop
Hope it helps :)

Detect what a python string begins with [duplicate]

This question already has answers here:
Checking whether a string starts with XXXX
(5 answers)
Closed 5 years ago.
To detect whether a python string ends with a particular substring, say ".txt", there is a convenient built-in python string method;
if file_string.endswith(".txt"):
I would like to to detect whether a python string begins with a particular substring, say "begin_like_this". I am not able find a convenient method that I can use like this;
if file_string.beginswith("begin_like_this"):
I am using Python 3.6.
You're looking for str.startswith
if file_string.startswith("begin_like_this"):

How to break long lines in python that have no brackets or operators? [duplicate]

This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 6 years ago.
This is different from similar questions I have found on this site as the code in question doesn't have any operators or brackets before the character limit to split easily on. I have several long lines in python without operators or brackets before 79 characters. As an example:
self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.remove_widget(self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.startbutton)
The above line has 72 characters before a bracket, and is within a function definition within a class so therefore with 4-char spacing per nest level, has 81 characters before a bracket.
What is the preferred way of dealing with this?
With your particular example, I expect if you first solved the problem of "make this code readable", fitting in 79 characters would come naturally.
That said, you can add brackets:
(self.caller.parent.parent
.parent.caller.parent.bar
.ids.actionview.remove_widget(
self.caller.parent.parent
.parent.caller.parent
.bar.ids.actionview.startbutton)
)

Categories