Python新手挑战 - 寻找更好的方法

时间:2017-10-27 20:29:35

标签: python-2.7

作为一个Python学习练习,我创建了一个脚本,用于从我的有线调制解调器的管理页面中抓取并记录状态,以便让我随时查看调制解调器的状态。该脚本功能正常,但我想提高效率,尤其是下面的功能,它需要记录每个下游通道的统计数据,使用正则表达式从数据点中删除非数字字符。收集。

正如您所看到的,它使用了许多近似相似的变量来处理每个下游通道的统计数据集。我知道在那里完成同样的任务是一种更加光滑的方式,但还不够精通,无法弄明白。

如果有人能提供更好的方法来启动我的Python理解,我将不胜感激。

def log_downstream_signal(soup, con, reading_ts):
    table = soup.find_all('table')[0]  # this is Downstream channel table
    rows = table.find_all('tr')  # get all the rows
    cols = rows[1].find_all('td')
    channelid1 = re.sub('[^0-9]*', '', cols[1].text)
    channelid2 = re.sub('[^0-9]*', '', cols[2].text)
    channelid3 = re.sub('[^0-9]*', '', cols[3].text)
    channelid4 = re.sub('[^0-9]*', '', cols[4].text)
    cols = rows[3].find_all('td')
    snr1 = re.sub('[^0-9]*', '', cols[1].text)
    snr2 = re.sub('[^0-9]*', '', cols[2].text)
    snr3 = re.sub('[^0-9]*', '', cols[3].text)
    snr4 = re.sub('[^0-9]*', '', cols[4].text)
    cols = rows[5].find_all('td')
    #Note the column indices for this row are offset by 1
    power1 = re.sub('[^0-9]*', '', cols[2].text)
    power2 = re.sub('[^0-9]*', '', cols[3].text)
    power3 = re.sub('[^0-9]*', '', cols[4].text)
    power4 = re.sub('[^0-9]*', '', cols[5].text)

    cur = con.cursor()
    sql = "INSERT INTO downstream_log VALUES ('" + reading_ts + \
        "','" + channelid1 + "'," + snr1 + "," + power1 + ")"
    cur.execute(sql)
    sql = "INSERT INTO downstream_log VALUES ('" + reading_ts + \
        "','" + channelid2 + "'," + snr2 + "," + power2 + ")"
    cur.execute(sql)
    sql = "INSERT INTO downstream_log VALUES ('" + reading_ts + \
        "','" + channelid3 + "'," + snr3 + "," + power3 + ")"
    cur.execute(sql)
    sql = "INSERT INTO downstream_log VALUES ('" + reading_ts + \
        "','" + channelid4 + "'," + snr4 + "," + power4 + ")"
    cur.execute(sql)
    cur.close()
    con.commit()

1 个答案:

答案 0 :(得分:0)

我能看到的一种方法是使用这段代码:

  channelid = re.sub('[^0-9]*', '', cols[1].text)
    channelid = re.sub('[^0-9]*', '', cols[2].text)
    channelid = re.sub('[^0-9]*', '', cols[3].text)
    channelid = re.sub('[^0-9]*', '', cols[4].text)
    cols = rows[3].find_all('td')
    snr1 = re.sub('[^0-9]*', '', cols[1].text)
    snr2 = re.sub('[^0-9]*', '', cols[2].text)
    snr3 = re.sub('[^0-9]*', '', cols[3].text)
    snr4 = re.sub('[^0-9]*', '', cols[4].text)
    cols = rows[5].find_all('td')
    power1 = re.sub('[^0-9]*', '', cols[2].text)
    power2 = re.sub('[^0-9]*', '', cols[3].text)
    power3 = re.sub('[^0-9]*', '', cols[4].text)
    power4 = re.sub('[^0-9]*', '', cols[5].text)

您可以使用for循环和列表来简化此操作

channelid, snr, power = [], [], []
channelCols = rows[1].find_all('td')
snrCols = rows[3].find_all('td')
powerCols = rows[5].find_all('td')
for i in range(1, 5):
     channelid.append(re.sub('[^0-9]*', '', channelCols[i].text))
     snr.append(re.sub('[^0-9]*', '', snrCols[i].text))
     power.append(re.sub('[0-9]*', '', powerCols[i+1].text))

cur = con.cursor()
for i in range(0, 4)
     sql = "INSERT INTO downstream_log VALUES ('" + reading_ts + \
    "','" + channelpi[ + "'," + snr[i] + "," + power[i] + ")"
     cur.execute(sql)
cur.close()
con.commit()