Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex replace

Tags:

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.

like image 487
Ciarán Avatar asked Nov 17 '11 19:11

Ciarán


People also ask

Can I use regex in replace Python?

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.

Can I use regex in replace?

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.

How do you replace all occurrences of a regex pattern in a string Python?

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.

What is regex in pandas replace?

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).


2 Answers

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.

like image 197
spowers Avatar answered Oct 22 '22 02:10

spowers


>>> 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'] 
like image 32
kojiro Avatar answered Oct 22 '22 00:10

kojiro