Si-World

vichare的硅基世界

从Google weather api读取天气预报的python脚本

September15
?View Code PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import urllib
from xml.dom import minidom
 
proc = lambda tnlist, conds : [ \
    [temp.getAttribute('data') for temp in conds.getElementsByTagName(tagname)][0] \
    for tagname in (tnlist) ]
 
data = urllib.urlopen('http://www.google.com/ig/api?weather=Beijing&hl=zh-cn').read().decode('gbk').encode('utf8')
xmldoc = minidom.parseString(data)
curconds = xmldoc.getElementsByTagName("current_conditions")
(t, h, c, w) = proc(('temp_c', 'humidity', 'condition', 'wind_condition'), curconds[0])
print t
print h
print c
print w
 
for cond in xmldoc.getElementsByTagName("forecast_conditions"):
    (d, l, h, c) = proc(('day_of_week', 'low', 'high', 'condition'), cond)
    print '%s: %s/%s %s' % (d, l, h, c)

利用union做类型转换

September10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include 
 
using namespace std;
 
union conv {double arg; struct fl{float real; float imag;}f;};
 
int main()
{
    float farr[] = {1.0, 2.0};
    double* pd = (double*)(farr);
    conv c;
    c.arg = *pd;
    cout << c.f.real << endl;
    cout << c.f.imag << endl;
    return 0;
}