Why a regex?
mystring = mystring.replace(",", "")
is enough.
Of course, if you insist:
mystring = re.sub(",", "", mystring)
but that's probably an order of magnitude slower.
Answer from Tim Pietzcker on Stack OverflowWhy a regex?
mystring = mystring.replace(",", "")
is enough.
Of course, if you insist:
mystring = re.sub(",", "", mystring)
but that's probably an order of magnitude slower.
You don't need RE for that trivial operation. just use replace() on string:
a="123,asd,wer"
a.replace(",", "")
The pattern you use, ([\w],[\s\w|\w]), is consuming a word char (= an alphanumeric or an underscore, [\w]) before a ,, then matches the comma, and then matches (and again, consumes) 1 character - a whitespace, a word character, or a literal | (as inside the character class, the pipe character is considered a literal pipe symbol, not alternation operator).
So, the main problem is that \w matches both letters and digits.
You can actually leverage lookarounds:
(?<=[a-zA-Z]),(?=[a-zA-Z\s])
See the regex demo
The (?<=[a-zA-Z]) is a positive lookbehind that requires a letter to be right before the , and (?=[a-zA-Z\s]) is a positive lookahead that requires a letter or whitespace to be present right after the comma.
Here is a Python demo:
import re
p = re.compile(r'(?<=[a-zA-Z]),(?=[a-zA-Z\s])')
test_str = "2015,1674,240/09,PEOPLE V. MICHAEL JORDAN,15,15\n2015,2135,602832/09,DOYLE V ICON, LLC,15,15"
result = p.sub("", test_str)
print(result)
If you still want to use \w, you can exclude digits and underscore from it using an opposite class \W inside a negated character class:
(?<=[^\W\d_]),(?=[^\W\d_]|\s)
See another regex demo
\w matches a-z,A-Z and 0-9, so your regex will replace all commas. You could try the following regex, and replace with \1\2.
([a-zA-Z]),(\s|[a-zA-Z])
Here is the DEMO.
Efficient Way To Remove Repeating Commas From String
Replacing comma in Python - Stack Overflow
python - re.sub replace spaces with comma - Stack Overflow
python - Is there a simple way to replace a comma with nothing? - Stack Overflow
I have a string that consists of something like:
my_string = 'Now is the time, , for all good men, , ,to come to the aid,, of their party'
...and I want to keep only a single comma for each repeating set of commas:
result = 'Now is the time, for all good men, to come to the aid, of their party'
I've looked a numerous methods to remove sequential characters, but 1) these are not sequential (may have one or more blanks between them); and 2) I only want to remove the extra commas and not other repeating characters that might appear in the string.
Any help would be greatly appreciated.
Replace them:
posts[0].replace(',', '')
Or use the locale module (if your locale's thousands delimiter is a comma):
import locale
locale.setlocale(locale.LC_ALL, '')
n = locale.atoi(posts[0])
I would advise against using just regex for scraping. Unless Posts: (.*?) is all you're after, parse the HTML with a HTML parser like lxml or BeautifulSoup.
>>> '1,092,391'
'1,092,391'
>>> '1,092,391'.replace(',', '')
'1092391'
>>> int('1,092,391'.replace(',', ''))
1092391
nothing I've found on here or Google has seemed to work
I’m having a hard time to believe that. A quick search for “Python string replace” should get you to str.replace very quickly, not to mention that searching it in the Python documentation gets you there even faster. The first result I get for “Python comma replace” is even a question on SO answering your problem.
And if everything failed, you could have used regular expressions which you apparently already know how to use.
To remove the leading and trailing spaces you can use .strip(), and then to replace consecutive whitespace characters using the regular expression \s+:
>>> import re
>>> s = " 2.4 -2.0 4.3"
>>> re.sub("\s+", ",", s.strip())
'2.4,-2.0,4.3'
Fastest way:
','.join(yourString.split())
Sample:
','.join(" a bcd e f ".split())
'a,bcd,e,f'
On a string you can replace any character, such as ,, like so:
s = "Hi, I'm a string"
s_new = s.replace(",", "")
Also, the comparisons you are doing on the strings may not always perform the way you expect. It may be better to cast to numeric values first. Something like:
for word in split:
n = float(word.replace(",", ""))
# do comparison on n, like
# if n >= 0: ...
As a tip, try reading in your file with with:
# ...
with open(fileName, 'r') as f:
for line in f:
# this will give you `line` as a string
# ending in '\n' (if it there is an endline)
string_wo_commas = line.replace(",", "")
# Do more stuff to the string, like cast to float and comparisons...
This is a more idiomatic way to read in a file and do something to each line.
Check out this: How do I use Python to convert a string to a number if it has commas in it as thousands separators? and this: How to delete a character from a string using python?
Also, note that your word >= ".0" comparisons are string comparisons, not numerical. They may not do what you think they will. For example:
>>> a = '1,250'
>>> b = '975'
>>> a > b
False