0

I have a py script which amongst other things parses a value from a HTML table using BeautifulSoup.

The value which is being returned (outTempReal) seems to have some whitespace after the value. I know this due to the print I use...

print "Temp 1 =", avgtemperatures[0],
print "Temp 2 =", avgtemperatures[1],
print "Temp 3 =", avgtemperatures[2],
print "Temp 4 =", avgtemperatures[3],
print "Temp 5 =", avgtemperatures[4],
print "Outside Temp =", outTempReal,
print "METAR Temp =", currentTemp,
print "Plant Room Temp =", avgtemperatures[5],
print "Flow Temp =", avgtemperatures[6],
print "Return Temp =", avgtemperatures[7]

Which returns the following...

Temp 1 = 79.625 Temp 2 = 79.1456666667 Temp 3 = 31.229 Temp 4 = 28.125 Temp 5 = 27.2706666667 Outside Temp = 4.8        METAR Temp = 5 Plant Room Temp = 16.7913333333 Flow Temp = 13.875 Return Temp = 18.312

You can see that after the Outside Temp = 4.8 there is whitespace before the next print value.

This is the code used to get the value in the first place...

table = soup.find('table')
    for row in table.findAll('tr')[1:]:
            col = row.findAll('td')
            if len(col) >= 2:
                    time = col[1].string
                    temp = col[2].string
    outTempReal = re.sub(r'[^0-9\-\d.\s+]',' ', temp)

I have tried the following two methods to remove the whitespace but no joy...

outTempReal.strip()
re.sub('\s+',' ',outTempReal)

I really need this value to be just the decimal number because it is used to update a RRD.

Can anyone help?

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • I don't know python but replacing `\s+` with ` ` does work for any whitespace. – tenub Jan 09 '14 at 19:08
  • Still the same... sorry! – user3176960 Jan 09 '14 at 21:09
  • It's not a problem with the regex though, but rather probably your way of implementing the replace. If I knew python I could help, I'm just telling you the regex is fine. Maybe you need to escape it with double slashes like `\\s+`? – tenub Jan 09 '14 at 21:45
  • This is where bach scripting will come in handy because you can do something like `tr -s ' '` Try this [link](http://stackoverflow.com/a/1185528/2089675) or [this](http://stackoverflow.com/a/8270146/2089675) for python version of it – smac89 Jan 10 '14 at 15:47

1 Answers1

0

.rstrip() will trim any trailing whitespace:

>>> thing = 'value              '
>>> thing
'value              '
>>> thing.rstrip()
'value'
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
  • Hi, Still returns the following from the print.... Temp 1 = 77.062 Temp 2 = 75.437 Temp 3 = 33.25 Temp 4 = 32.187 Temp 5 = 31.0 Outside Temp = 4.5 METAR Temp = 5 Plant Room Temp = 16.875 Flow Temp = 13.375 Return Temp = 34.5413333333 So it looks like it doesn't work. Could this be something other than 'whitespace' ? – user3176960 Jan 09 '14 at 20:25
  • Looks like the formatting of the comments does not show the space. It's still there! – user3176960 Jan 09 '14 at 20:26