Hey I'm trying to figure out a regular expression to do the following.
Here is my string
Place,08/09/2010,"15,531","2,909",650
I need to split this string by the comma's. Though due to the comma's used in the numerical data fields the split doesn't work correctly. So I want to remove the comma's in the numbers before running splitting the string.
Thanks.
Regex can be used to perform various tasks in Python. It is used to do a search and replace operations, replace patterns in text, check if a string contains the specific pattern.
How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.
sub() method will replace all pattern occurrences in the target string. By setting the count=1 inside a re. sub() we can replace only the first occurrence of a pattern in the target string with another string. Set the count value to the number of replacements you want to perform.
replace() Pandas replace() is a very rich function that is used to replace a string, regex, dictionary, list, and series from the DataFrame. The values of the DataFrame can be replaced with other values dynamically. It is capable of working with the Python regex(regular expression).
new_string = re.sub(r'"(\d+),(\d+)"', r'\1.\2', original_string)
This will substitute the ,
inside the quotes with a .
and you can now just use the strings split method.
>>> from StringIO import StringIO >>> import csv >>> r = csv.reader(StringIO('Place,08/09/2010,"15,531","2,909",650')) >>> r.next() ['Place', '08/09/2010', '15,531', '2,909', '650']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With