I should be extracting the float 0.875, but I get 0.0
I don’t know why. I’ll post my code below and a link to the challenge. I can’t figure out why I’m not getting the correct answer. Can you help please?
handle = open('mbox-short.txt')
numlst = list()
for line in handle:
line = line.rstrip()
stuff = re.findall('^X-DSPAM-Confidence: ([0-9]+)', line)
if len(stuff) != 1: continue
num = float(stuff)
print('Maximum: ', max(numlst))
If I print
numlst by itself:
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]’
What will your regular expression do if it encounters a decimal? eg. 0.85
So the problem was I didn’t include the decimal. I did and it worked but I have a question/clarification. Please correct me if my assumption is wrong.
stuff = re.findall('^X-DSPAM-Confidence: ([0-9].+)', line)
This checks for any string beginning with ‘X-DSPAM-Confidence:’ that is followed by a colon and any amoung of numbers 0-9 followed by or including a decimal.
stuff = re.findall('^X-DSPAM-Confidence: ([0-9].)+', line)
This checks for any amount of numbers followed by a decimal.
For A. I get ‘.0997’,
B I get ‘78.0’
Is my explanation correct?
Not quite, but I can see the logic there.
. matches any character, not a decimal. It’s capturing the last 2 digits of those lines
Just the 75
You can use this great site to test your expressions and it will highlight and explain the results: https://regex101.com/
The square bracket is like OR.
if i want to catch also a character ‘B’, i would just throw it in the bracket.
if i want to catch also characters of ‘A’ or ‘C’. i would do:
you can read more at here >Python Regex at W3schools