This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I would like to know how can I get from a string and using reg expressions all values until the comma starting from the end. See below example, I would like to get the value "CA 0.810" into a variable:
prue ="VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"
So far, I have the below code:
test = re.findall('([0-9]+)$',prue)
print test
However, I only get below output:
['810']
Could you please advise how can I get "CA 0.810" into the test variable?
You can do this using the split method. From the docs, it will:
Return a list of the words in the string, using sep as the delimiter string.
So if you can take your string:
prue = "VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"
you can do :
prue.split(",")
which will return a list of the strings split by the commas:
['VA=-0.850', '0.800;CA=-0.863', '0.800;SP=-0.860', '0.810;MO=-0.860', '0.810;SUN=MO -0.850', 'CA 0.810']
So if you just want the last item ('CA 0.8101') into a variable named test, you can just take the last element from the list by indexing with -1:
test = prue.split(",")[-1]
test is now: 'CA 0.810'
Hope this helps!
Related
This question already has answers here:
Python split() without removing the delimiter [duplicate]
(4 answers)
Closed 11 months ago.
Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.
How about :
string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)
Output :
['a!', 'ab!', 'b!']
split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this
s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]
# output : ["a!", "b!"]
This question already has answers here:
Index-wise Replacement of String Data in Python
(1 answer)
String replace in Python, in sequence, by index [duplicate]
(1 answer)
Closed 1 year ago.
I'm a newbie who just Started learning Python from YouTube. I am trying to make a program to replace old string Numbers with new string Numbers and facing problems while replacing numbers. Want to replace index-wise (What is its technical term (I don't know)). It can go in one direction or index-wise.
my string is = (001001001001001001001001001001001001001101100100110110011011001101011010011010110011011)
and I want to replace 101 with 01, 1101 with 11, 1001 with 011, and 11001 with 111,
so my replaced string/output string will be like this..
(00011000110001100011000110001100110110011011010110101100110111011)
As per python's normal string replace method it Cant work Anyone can help my
string = "001001001001001001001001001001001001001101100100110110011011001101011010011010110011011"
string = string.replace('101', '01').replace('1101', '11').replace('1001', '011').replace('11001', '111')
fin.close()
fin = open("2x.txt", "wt")
fin.write(string)
fin.close()
(00011000110001100011000110001100110110011011010110101100110111011)
In general python you can't "edit" strings, you need to create new ones. E.g:
my_old_string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
# use .replace()
my_new_string = my_old_string.replace('010', '0')
You could achieve the same thing with a single variable aswell:
string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
string = string.replace('010', '0')
string = string.replace('1101', '11')
# continue with this as often as you want
I am not sure, if your "doing all in one line" syntax is valid
This question already has answers here:
Python - Regular expressions get numbers between parenthesis
(2 answers)
Closed 2 years ago.
I have a column with values like this:
4 (3 in force)
44 (39 in force)
I was able to use this to get a new column for the first number.
df['new'] = df['column'].str.extract('(\d+)')
How can I get a new column for the second number? (3,39, etc.)
One way that specifically answers you question would be to use a lookbehind regular expression, that basically says "the first number after another number, a space and a parenthesis":
df['new'] = df['column'].str.extract('(?<=\d+\s\()\d+')
But if you're extracting multiple parts from a single string, you might consider combining the two and using groups in the regex to access the parts you want.
You could just take the row, convert it into a string, split it, and access the needed numbers, for example:
row = '4 (3 in force)'
row.split(' ') # This returns ['4', '(3', 'in', 'force)']
row.split(' ')[1] # This returns '(3'
row.split(' ')[1][1:] # And this returns all numbers after the bracket, so '3'
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 2 years ago.
I am kind of noob in python and struck in middle of code. I want to trim my string.
For example- my string is "bangalore store 1321" and i want to trim it to "banglore"
Looks like you want to keep the first word (which is not "trimming" though). So you do two things
break the string into a list of words (where "word" is something separated by spaces)
take the first element of that list
words = mystring.split(' ')
result = words[0]
For a slicing answer:
def sub_string(str, start,end):
return str[start:end]
You can also use split, by definition, this splits by spaces, if any other delimiter needed, you can identity it inside the split arguments split(',')
def split_string(str):
return str.split()
This function will return an array of strings. Choose whichever you want from this array
str="bangalore store 1321"
print(str.split(' ')[0])
Output
bangalore
You can use str's partition method to avoid creating a list like str.split
>>> first_word, _, _ = s.partition(' ') # _ is a convention for a throwaway variable
>>> print(first_word)
bangalore
str.partition takes one argument - the separator - and returns the parts of the string before and after the first occurrence of the separator.
This question already has answers here:
Python Regex to find a string in double quotes within a string
(6 answers)
Closed 6 years ago.
I'm trying to write a function where the input has a keyword that occurs multiple times in a string and will print the stuff that has double quotation marks between them after the keyword. Essentially...
Input= 'alkfjjiekeyword "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee keyword"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness'
Output= someonehelpmepls
itonlygivesmeoneinsteadofmultiple
If its possible to have the outputs as its own line that would be better.
Here's what I have so far:
def getEm(s):
h = s.find('keyword')
if h == -1
return -1
else:
begin = s.find('"',h)
end = s.find('"', begin+1)
result = s[begin +1:end]
print (result)
Please don't suggest import. I do not know how to do that nor know what it is, I am a beginner.
Let's take some sample input:
>>> Input= 'alkfjjiekeyword "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee keyword"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness'
I believe that one " was missing from the sample input, so I added it.
As I understand it, you want to get the strings in double-quotes that follow the word keyword. If that is the case, then:
def get_quoted_after_keyword(input):
results = []
split_by_keyword = input.split('keyword')
# you said no results before the keyword
for s in split_by_keyword[1:]:
split_by_quote = s.split('"')
if len(split_by_quote) > 1:
# assuming you want exactly one quoted result per keyword
results.append(split_by_quote[1])
return results
>print('\n'.join(get_quoted_after_keyword(Input))
>someonehelpmepls
>itonlygivesmeoneinsteadofmultiple
How it works
Let's look at the first piece:
>>> Input.split('keyword')
['alkfjjie',
' "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee ',
'"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness']
By splitting Input on keyword, we get, in this case, three strings. The second string to the last are all strings that follow the word keyword. To get those strings without the first string, we use subscripting:
>>> Input.split('keyword')[1:]
[' "someonehelpmepls"fjioee... omgsos someonerandom help helpppmeeeeeee ',
'"itonlygivesmeoneinsteadofmultiple"... sadnesssadness!sadness']
Now, our next task is to get the part of these strings that is in double-quotes. To do that, we split each of these strings on ". The second string, the one numbered 1, will be the string in double quotes. As a simpler example, let's take these strings:
>>> [s.split('"')[1] for s in ('"one"otherstuff', ' "two"morestuff')]
['one', 'two']
Next, we put these two steps together:
>>> [s.split('"')[1] for s in Input.split('keyword')[1:]]
['someonehelpmepls', 'itonlygivesmeoneinsteadofmultiple']
We now have the strings that we want. The last step is to print them out nicely, one per line:
>>> print('\n'.join(s.split('"')[1] for s in Input.split('keyword')[1:]))
someonehelpmepls
itonlygivesmeoneinsteadofmultiple
Limitation: this approach assumes that keyword never appears inside the double-quoted strings.