How to extract decimal number from currency using Regex in Python? [duplicate] - python

This question already has answers here:
Python Remove Comma In Dollar Amount
(4 answers)
Closed 2 years ago.
I have a list of currency in string. I want to extract only the decimal numbers from that list.
how can I do so ?
I tried the below code but it gives me an extra point in front of each value.
list1 = ['Rs.35,916.00', 'Rs.35,916.00', 'Rs.45,000.00']
for i in list1:
value = (sub(r'[^\d.]', '', i))
print(value)
Output:
.35916.00
.35916.00
.45000.00
expected output:
35916.00
35916.00
45000.00

list1 = ['Rs.35,916.00', 'Rs.35,916.00', 'Rs.45,000.00']
for i in list1:
value = (sub(r'Rs\.|[^\d.]', '', i))
print(value)

Related

How to get a string after and before a specific substring in Python? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]

How to use strip in the middle of a string [duplicate]

This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Remove specific characters from a string in Python
(26 answers)
Closed 3 years ago.
Given this string, ###hi##python##python###, how can I remove all instances of '#'?
v = "###hi#python#python###"
x = v.strip("#")
print(x)
Expected output: "hipythonpython"
Just use replace
the_str = '###hi##python##python###'
clean_str = the_str.replace('#','')
print(clean_str)
Output
hipythonpython
What you want is not strip, but replace:
v = '###hi#python#python###'
x = v.replace('#', '')
print(x)
Output:
hipythonpython

Extract float numbers from string in python [duplicate]

This question already has answers here:
How to detect a floating point number using a regular expression
(7 answers)
python: extract float from a python list of string( AUD 31.99)
(5 answers)
Closed 4 years ago.
i want to extract the numbers from the following string:
FRESENIUS44.42 BAYER64.90 FRESENIUS MEDICAL CARE59.12 COVESTRO45.34 BASF63.19
I've tried the following approach but that didn't work:
l = []
for t in xs.split():
try:
l.append(float(t))
except ValueError:
pass
The result should be 44.42 64.90 59.12 45.34 63.19
Thank you!
import re
list = ["FRESENIUS44.42", "BAYER64.90" "FRESENIUS MEDICAL CARE59.12", "COVESTRO45.34", "BASF63.19",]
newList = [float(re.findall("\d+\.\d+", i)[0]) for i in list]
print(newList)
First, we extract the floats using regex, then we convert into floats and append to list using list comprehension.
import re
myStr = 'FRESENIUS44.42 BAYER64.90 FRESENIUS MEDICAL CARE59.12 COVESTRO45.34 BASF63.19'
outList = re.findall(r"[-+]?\d*\.\d+|\d+", myStr)
['44.42', '64.90', '59.12', '45.34', '63.19']
finalStr = ' '.join(outList)
'44.42 64.90 59.12 45.34 63.19'

How to subtract multiple string values from another string [duplicate]

This question already has answers here:
Replacing specific words in a string (Python)
(4 answers)
Closed 6 years ago.
With:
abc = 'abc'
xyz = 'xyz'
word = 'begin abc- middle_xyz_ end'
I need to extract the values of abc and xyz from word.
The result should be
result = 'begin - middle__ end'
How to achieve this with a minimum amount of code?
You use replace() with an empty string as the value to replace with.
result = word.replace('abc','').replace('xyz','')

Python - how to remove commas, square brackets and quotation marks from list [duplicate]

This question already has answers here:
Pythonic way to print list items
(13 answers)
Closed 7 years ago.
How do I remove the quote marks and commas and brackets from this result:
encrypt = input("enter your string: ")
encrypt = encrypt.replace(" ","")
encrypt_list = [encrypt[i:i+5] for i in range(0, len(encrypt), 5)]
print (encrypt)
print (encrypt_list)
If the input was: 5 blocks of text test
The output is: ['5bloc', 'ksoft', 'extte', 'st']
I need it to be: 5bloc ksoft extte st
You can use str.join, like this:
>>> s = ' '.join(['5bloc', 'ksoft', 'extte', 'st'])
>>> print(s)
5bloc ksoft extte st

Categories