首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python新手挑战-寻找更好的方法

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

Stack Overflow用户
提问于 2017-10-27 20:29:35
回答 1查看 30关注 0票数 1

作为Python学习练习,我创建了一个脚本,用于从电缆调制解调器的管理页面中抓取和记录状态,以允许我查看调制解调器的状态随时间的推移。脚本是功能性的,但是我想提高效率,特别是下面的函数,它需要记录每个下游通道的统计信息,使用正则表达式从我收集的数据点中去掉非数字字符。

正如您所看到的,它使用了许多类似名称的变量来处理每个下游通道的统计数据集。我知道在完成同样的任务时,肯定会有一种更圆滑的方式,但我还没有足够的能力去解决这个问题。

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-27 20:36:18

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

代码语言:javascript
复制
  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循环和列表简化这一过程。

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46982958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档