I have a number series contained in a string, and I want to remove everything but the number series. But the double quotes are giving me errors. Here are examples of the strings and a sample command that I have used. All I want is 127.60-02-15, 127.60-02-16, etc.
JavaScript
x
3
1
<span id="lblTaxMapNum">127.60-02-15</span>
2
<span id="lblTaxMapNum">127.60-02-16</span>
3
I have tried all sorts of methods (e.g., triple double quotes, single quotes, quotes with backslashes, etc.). Here is one inelegant way that still isn’t working because it’s still leaving “>:
JavaScript
1
4
1
text = text.replace("<span id=", "")
2
text = text.replace(""lblTaxMapNum"", "")
3
text = text.replace("</span>", "")
4
Here is what I am working with (more specific code). I’m retrieving the data from an CSV and just trying to clean it up.
JavaScript
1
9
1
text = open("outputA.csv", "r")
2
text = ''.join([i for i in text])
3
text = text.replace("<span id=", "")
4
text = text.replace(""lblTaxMapNum"", "")
5
text = text.replace("</span>", "")
6
outputB = open("outputB.csv", "w")
7
outputB.writelines(text)
8
outputB.close()
9
Advertisement
Answer
If you add a >
in the second replace
it is still not elegant but it works:
JavaScript
1
4
1
text = text.replace("<span id=", "")
2
text = text.replace(""lblTaxMapNum">", "")
3
text = text.replace("</span>", "")
4
Alternatively, you could use a regex:
JavaScript
1
9
1
import re
2
3
text = "<span id="lblTaxMapNum">127.60-02-16</span>"
4
5
pattern = r".*>(d*.d*-d*-d*)D*" # the pattern in the brackets matches the number
6
match = re.search(pattern, text) # this searches for the pattern in the text
7
8
print(match.group(1)) # this prints out only the number
9