Split on your separator at most once, and take the first piece:
sep = '...'
stripped = text.split(sep, 1)[0]
You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
Answer from Ned Batchelder on Stack OverflowSplit on your separator at most once, and take the first piece:
sep = '...'
stripped = text.split(sep, 1)[0]
You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
Assuming your separator is '...', but it can be any string.
text = 'some string... this part will be removed.'
head, sep, tail = text.partition('...')
>>> print head
some string
If the separator is not found, head will contain all of the original string.
The partition function was added in Python 2.5.
S.partition(sep)->(head, sep, tail)Searches for the separator sep in S, and returns the part before it, the separator itself, and the part after it. If the separator is not found, returns S and two empty strings.
How can I remove everything in a string until a character(s) are seen in Python - Stack Overflow
Removing all but one instance of a phrase from a string
Python: Remove everything after first comma in string
Removing everything before/after a particular character
Hello,
I want to remove the entire substring after a particular word in Python.
For instance, if the word is "Excerpt," all text after the word would be removed.
However, my code is not accomplishing that task.
If the word exists in the string, I would find the index of it.
After that, I would subtract the len(text)-index of the word, which should point to the word.
Am I not returning from 0-index of the word, hence removing the word, and everything after the word.
What is the issue with my code?
str1 = 'Excerpt'
if str1 in text.split():
strLength = text.find(str1)
a = text[0:len(text)-strLength]
print(a)
You can use str.find method with a simple indexing :
>>> s="have an egg please"
>>> s[s.find('egg'):]
'egg please'
Note that str.find will returns -1 if it doesn't find the sub string and will returns the last character of your string.So if you are not sure that always your string is contain the sub string you better to check the value of str.find before using it.
>>> def slicer(my_str,sub):
... index=my_str.find(sub)
... if index !=-1 :
... return my_str[index:]
... else :
... raise Exception('Sub string not found!')
...
>>>
>>> slicer(s,'egg')
'egg please'
>>> slicer(s,'apple')
Sub string not found!
string = 'Stack Overflow'
index = string.find('Over') #stores the index of a substring or char
string[:index] #returns the chars before the seen char or substring
Hence, the output will be
'Stack '
and
string[index:]
will give
'Overflow'
filename.txt
years list last_5
2022; year - current, 100 users, 5 left
2021; year - previous, 78 users, 6 left
2020; year - prior, 60 users , 1 left
2019; year - prior, 55 users
2018; year - prior, 28 users
new
years list next_5
2017; year - prior, 12 users
2016; year - prior, 6 users
2015; year - prior, 5 users
2014; year - prior, 4 users
2013; year - prior, 4 users
newcurrent code
with open (‘filename.txt’, ‘r+’) as f:
lines = f.readlines()
f.seek(0)
f.writelines(line.strip(“\t”) for line in lines if line.strip() and line.strip(“\n”) != “new”)
f.truncate()current output
years list last_5 2022; year - current, 100 users, 5 left 2021; year - previous, 78 users, 6 left 2020; year - prior, 60 users , 1 left 2019; year - prior, 55 users 2018; year - prior, 28 users years list next_5 2017; year - prior, 12 users 2016; year - prior, 6 users 2015; year - prior, 5 users 2014; year - prior, 4 users 2013; year - prior, 4 users
expected output
years list last_5 2022; year - current 2021; year - previous 2020; year - prior 2019; year - prior 2018; year - prior years list next_5 2017; year - prior 2016; year - prior 2015; year - prior 2014; year - prior 2013; year - prior
I’ve seen that .split(“,”) splits the string using the comma and I tried adding .split(“,”, 1) to the line.strip before and after the for loop but this doesn’t appear to work.
Can anyone help me?
Here is what i want: https://pastebin.com/yQDjXbFX
-
thanks in advanced
You can split a string by any character, in this case a question mark. This returns a list of the part before and after the splitted character.
>>> s = "https://www.instagram.com/p/BY4MTdWgdPH/?hl=en&taken-by=nike"
>>> s.split("?")[0]
'https://www.instagram.com/p/BY4MTdWgdPH/'
Please use https://docs.python.org/3/library/urllib.parse.html instead of any of the regex-based suggestions that have been posted thus far. It's a much cleaner and resilient method.