python gdata, extracting numbers from output -


i'm still new python , i've been working script system info raspberry pi, cpu temp , such , import google doc spreadsheet. goal extract numbers output, in form temp=54.1'c. need numbers alone able graph data on time...

i'm using:

import gdata.spreadsheet.service import os import subprocess import re  email = 'myemail@gmail.com' password = 'mypassword'  spreadsheet_key = 'sjdaf;ljaslfjasljdgasjdflasdjfgkjvja' worksheet_id = '1'  def temp():    command = "/opt/vc/bin/vcgencmd measure_temp"    proc = subprocess.popen(command, stdout=subprocess.pipe, shell=true)    output = proc.stdout.read()    return output  def main():    spr_client = gdata.spreadsheet.service.spreadsheetsservice()    spr_client.email = email    spr_client.password = password    spr_client.programmaticlogin()     dict = {}    dict['temp'] = temp()     entry = spr_client.insertrow(dict, spreadsheet_key, worksheet_id)  if __name__ == '__main__':       try:          main()       except:          print "insert row failed!" 

this above gives standard result. i've tried tinkering re.findall(), can't either right placement or right combination of conditions (r,'/d+', s , other things) return number 54.1... end "insert row failed"

any guidance appreciated. thanks!

you on right track using re; best bet (assuming decimal can arbitrary, etc.) this:

import re  def temp():     command = "/opt/vc/bin/vcgencmd measure_temp"     proc = subprocess.popen(command, stdout=subprocess.pipe, shell=true)     output = proc.stdout.read()      # build regex. use () capture group; want number of     # digits \d or decimal points \. preceded temp= ,     # followed 'c     temp_regex = re.compile(r'temp=([\d\.]*)\'c')     matches = re.findall(temp_regex, output)   # matches = ['54.1']      temp = float(matches[0])     return temp 

the regex captures combination of numbers , decimal places (e.g. 12.34.56 matched); restrict if necessary allow single decimal place, that's more work appears worth, if can trust data you're getting well-formed. if want number more precise, compile regex (for @ least 1 numeral preceding decimal place , 1 following it):

temp_regex = re.compile(r'temp=(\d+.\d)\'c') 

again, capture expression using parentheses (captured groups returned findall), time, increase specificity of we're looking for. capture number 123.4 not .4 , not 123. if find need broaden out bit still want 1 decimal place:

temp_regex = re.compile(r'temp=(\d+.\d+)\'c') 

that capture number @ least 1 numeral proceeding , following decimal, 1234.5678 match 1234. not , .1234 not.

as alternative using re.findall(), might use re.match(), returns match objects. usage (using direct method, rather pre-compiling string:

match = re.match(r'temp=(\d+.\d+)\'c', output) if match:     temp = float(match.group(1))   # first matching group captured () else:     pass   # should add error handling here 

one of things makes clearer way had re.findall() above if nothing captured, have issue, , need figure out how handle it.


you can @ other ways vary @ regular-expressions.info, best site i've found on web quick resource on topic.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -