0
regex= '<th scope="row" width="48%">52wk Range:</th><td class="yfnc_tabledata1"><span>(.+?)</span> - <span>(.+?)</span></td>'
    pattern = re.compile(regex)
LBUB = re.findall(pattern,htmltext)

I am trying to do basic data scraping in Python and perform some calculations on the returned real numbers. I have shown a small extract from the program so you can get the basic idea. I want it to read a html file and return certain numbers. The issue is that the real numbers are returned within a string variable like this...

[('90.77', '134.54')]

I want to extract the numbers from the variable so that they can be used as separate float variable. Does anybody know how to extract the two real numbers from within the string variable, basically getting rid of the ')], This is in Python 2.7.10

2 Answers 2

1

if you are getting the array then you could just use the Float() function and put the array index in. for example:

    StrFloats = [("90.77","134.54")]
FltNewNums = {}
IntInd = 0
for IntX in range(0,len(StrFloats)):
    for IntY in range(0,len(StrFloats[IntX])):
        FltNewNums[IntInd] = float(StrFloats[IntX][IntY])
        IntInd += 1

then you have the float variable in an array

i had to make a few changes. forgot about the tuple :/ this should work :)

Sign up to request clarification or add additional context in comments.

2 Comments

It can't handle having the symbols in the string so it just comes up as an error.
what symbols, are you getting the array they you put in the question? [('90.77', '134.54')]
0

This looks like the job for map

list(map(lambda t: (float(t[0]), float(t[1])), LBUB))

To avoid TypeError while casting to float you can use narrower capturing groups.

Something like:

(\d+\.\d+)

Anyway, parsing HTML with regular expressions is generally not a good idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.