小编ron*_*nak的帖子

Python:URLError:<urlopen error [Errno 10060]

操作系统:Windows 7; Python 2.7.3使用Python GUI Shell

我正在尝试通过Python阅读网站,有几位作者使用urlliburllib2库.为了将网站存储在变量中,我看到了类似的方法:

import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g)
Run Code Online (Sandbox Code Playgroud)

最后一行在120秒后生成错误:

> Traceback (most recent call last):   File "<pyshell#27>", line 1, in
> <module>
>     r = urllib2.urlopen(o)   File "C:\Python27\lib\urllib2.py", line 126, in urlopen
>     return _opener.open(url, data, timeout)   File "C:\Python27\lib\urllib2.py", line 400, in open
>     response = self._open(req, data)   File "C:\Python27\lib\urllib2.py", line 418, in _open
>     '_open', req)   File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
>     result = func(*args) …
Run Code Online (Sandbox Code Playgroud)

python urllib urllib2 python-2.7

15
推荐指数
2
解决办法
9万
查看次数

在PostgreSQL中提取xml标记的值

下面是我的Postgres表中的列响应.我想从Postgres数据库中的所有行中提取状态.状态可能有不同的大小SUCCESS,所以我不想使用子字符串函数.有办法吗?

<?xml version="1.0" ?><response><status>ERROR_MISSING_DATA</status><responseType>COUNTRY_MISSING</responseType><country_info>USA</country_info><phone_country_code>1234</phone_country_code></response>
Run Code Online (Sandbox Code Playgroud)

所以我的表格结构是这样的

   Column    |            Type             |                        Modifiers                         

-------------+-----------------------------+----------------------------------------------------------

 id          | bigint                      | not null default nextval('events_id_seq'::regclass)
 hostname    | text                        | not null
 time        | timestamp without time zone | not null
 trn_type    | text                        | 
 db_ret_code | text                        | 
 request     | text                        | 
 response    | text                        | 
 wait_time   | text                        | 
Run Code Online (Sandbox Code Playgroud)

我想从每个请求中提取状态.我该怎么做呢?

下面是一个示例行.并假设表名为abc_events

id          | 1870667
hostname    | abcd.local
time        | 2013-04-16 00:00:23.861
trn_type    | A
request     | <?xml version="1.0" ?><response><status>ERROR_MISSING_DATA</status><responseType>COUNTRY_MISSING</responseType><country_info>USA</country_info><phone_country_code>1234</phone_country_code></response>
response    | <?xml version="1.0" …
Run Code Online (Sandbox Code Playgroud)

xml postgresql xpath casting xml-parsing

11
推荐指数
1
解决办法
2万
查看次数

将两个列表合并到一个字典中时保留排序

我使用mysqldb连接到mysql数据库,我获取变量中的元数据/列和另一个中的数据/行.现在我必须将列表和元组合并为一个字典并保留顺序.我知道dicts是无序的,但除此之外还有其他选择吗?

cursor.description = ['userid', 'cid', 'mid', 'did', 'msid']
data = (29L, 35L, None, '', None)

result = {}
for i in data:
    result.update = dict(zip(cols, i))
Run Code Online (Sandbox Code Playgroud)

预期结果

result = {'userid': 29L, 'cid': 35L, 'mid': None, 'did': '', 'msid': None}
Run Code Online (Sandbox Code Playgroud)

python dictionary tuples list mysql-python

8
推荐指数
1
解决办法
6553
查看次数

mysqldb将时间戳数据转换为无

我正在使用MySQLdb与mysql数据库通信,我能够动态检索所有结果集.

我的问题是,一旦我得到结果集,有几列在mysql中被声明为时间戳,但是当它被检索时,它会变为无.

我有两列,两个都是声明的时间戳,但一个返回正确的数据,而其他返回None.utime和enddate都被声明为时间戳,但是当enddate执行时utime没有正确返回.

['utime', 'userstr', 'vstr_client', 'enddate']

((None, '000102030ff43260gg0809000000000004', '7.7.0', '1970-01-01 12:00:00.000000'))

def parse_data_and_description(cursor, data):

    res = []
    cols = [d[0] for d in cursor.description]
    print cols
    print data

    for i in data:
        res.append(OrderedDict(zip(cols, i)))
    return res

def call_multi_rs(sp, args):

    rs_id=0;
    conn = connect()
    cursor = conn.cursor()
    try:
        conn.autocommit(True)
        cursor.execute ("CALL %s%s" % (sp, args))
        while True:
            rs_id+=1
            data = cursor.fetchone( )
            listout = parse_data_and_description(cursor, data)
            print listout
            if cursor.nextset( )==None:
            # This means no more recordsets available
            break
Run Code Online (Sandbox Code Playgroud)

datetime timestamp type-conversion mysql-python

7
推荐指数
1
解决办法
1211
查看次数

从测试中计算最终成绩

Lloyd = {
    "name":"Lloyd",
    "homework": [90,97,75,92],
    "quizzes": [ 88,40,94],
    "tests": [ 75,90]
    }
Alice = {
    "name":"Alice",
    "homework": [100,92,98,100],
    "quizzes": [82,83,91],
    "tests": [89,97]
    }
Tyler = {
    "name":"Tyler",
    "homework": [0,87,75,22],
    "quizzes": [0,75,78],
    "tests": [100,100]
    }

def average(value):
    avg=0
    items = len(value)
    for item in value:
        avg +=item 
    return avg/items

def getAverage(dictin):
    hw = average(dictin.get('homework'))
    quiz = average(dictin.get('quizzes'))
    tests = average(dictin.get('tests'))
    weighted_average = hw*.1 + quiz*.3 + tests*.6
    return weighted_average

def getLetterGrade(score):
    if score >=90:
        return "A"
    elif score < 90 …
Run Code Online (Sandbox Code Playgroud)

python python-2.x python-2.7

0
推荐指数
1
解决办法
1630
查看次数