Frage

Using this code I have to get the result of PSI : 101-121 (Unhealthy). I need the value of 101 and 121 from var_psi to calculate average. Is there anyone that knows how to extract the 101 and 121 that can guide me along ? Thanks in advance =)

import xml.dom.minidom, xml.sax.saxutils
import logging
import httplib
from socket import timeout
import datetime
import time
import urllib2
import sys, os, platform, re
import sched, time
import simplejson as json
import urllib
from xml.dom import minidom
from urllib2 import urlopen

var_xml = urlopen("http://app2.nea.gov.sg/data/rss/nea_psi.xml")
var_all = xml.dom.minidom.parse(var_xml)

def extract_content(var_all, var_tag, var_loop_count):
    return var_all.firstChild.getElementsByTagName(var_tag)[var_loop_count].firstChild.data

var_loop_count = 0
var_item = " "
while len(var_item) > 0:
    var_title = extract_content(var_all, "title", var_loop_count)
    var_date = extract_content(var_all, "pubDate", var_loop_count)
    var_psi = extract_content(var_all, "psi", var_loop_count)
    var_psi1= extract_content(var_all, "psi", var_loop_count)


    print var_psi

    var_loop_count += 1
    break

    try:
        var_item = var_all.firstChild.getElementsByTagName("item")[var_loop_count].firstChild.data
    except:      
        var_item = ""
War es hilfreich?

Lösung

You could use a regular expression:

import re

psi_values = re.compile(r'PSI.*?(\d+)-(\d+)')

psi_low, psi_high = psi_values.search(var_psi).groups()

If you need the values to be integers, use map(int, ...) to apply the int() function to each matched group:

psi_low, psi_high = map(int, psi_values.search(var_psi).groups())

For your code sample, that gives me:

>>> psi_low, psi_high
(101, 121)

Andere Tipps

You could use the re module for that:

>>> import re
>>> test_string = '101-121 (Unhealthy)'
>>> psi_re = re.compile(r'(\d+)-(\d+) \(Unhealthy\)')
>>> psi_re.match(test_string)
<_sre.SRE_Match object at 0xb23140>
>>> psi_re.match(test_string).groups()
('101', '121')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top