python错误无法将字符串转换为float

时间:2017-12-11 07:10:56

标签: python slack

我们在将数据从松弛通道迁移到文件时会出现以下错误,当我们执行脚本以获取一天的数据时,它会完美执行。

但是当我们执行2个月数据的脚本时,它会在单独的文件中提供10天的数据,但会在特定日期抛出错误。松弛的源数据可能与预期的

有点不同
Traceback (most recent call last):
  File "C:\Users\Slack SCript\script.py", line 218, in <module>
    main()
  File "C:\Users\Slack SCript\script.py", line 201, in main
    parse(message['text'])
  File "C:\Users\Slack SCript\script.py", line 114, in parse
    size = float(elements[1])
ValueError: could not convert string to float: 

根据源数据,我们发现某些值0可能是因为此值而得到的错误。有没有办法跳过或继续未来。

enter image description here

from slackclient import SlackClient
import time
import os
import sys
import datetime
from dateutil.relativedelta import relativedelta

servers = ("fd2a", "ff1a", "hh3b", "kw1a", "kw1b", "lo8a", "os5a", "os5b", "sg2a", "sg2b", 'sy1a', 'va1a', 'va1b')
types = ("", "nfs", "cluster")

currser = "d"
currtype = ""
used = {}
total = {}
available = {}
ts = 0

dir_name = "data"


def savedata(dir_path, filename, data):
    f = open(dir_path + filename, "w")  # opens file with name of "test.txt"
    print(dir_path + filename)
    f.write(data)
    f.close()


def reset_data():
    print("datareset")
    for i in range(0, len(servers)):
        for j in range(0, len(types)):
            used[servers[i] + types[j]] = 0
            total[servers[i] + types[j]] = 0
            available[servers[i] + types[j]] = 0


def write_data(ts):
    datastr = ''
    global used
    global total
    ttotaltotalsum = 0

    for j in range(0, len(types)):

        datastr += types[j] + '\n'
        datastr += "Name\t" + "Region\t" + "total(TB)\t" + "used(TB)\t" + "available(TB)\t" + "Used(%)\n"
        for i in range(0, len(servers)):
            tused = used[servers[i] + types[j]]
            ttotal = total[servers[i] + types[j]]
            ttotaltotalsum += ttotal
            if (ttotal != 0):
                datastr += (
                    servers[i][0:len(servers[i]) - 1] + "\t\t" +
                    servers[i][len(servers[i]) - 1] + "\t\t" +

                    "{:.1f}".format(ttotal / 1024) + " \t\t" +
                    "{:.1f}".format(tused / 1024) + " \t\t" +
                    "{:.1f}".format((ttotal - tused) / 1024) +"\t\t"+
                    "{:.1f}".format(tused / ttotal * 100) + " \t\t" +

                    "  \n")

    print("..")
    if (ttotaltotalsum > 0):
        hour= datetime.datetime.fromtimestamp(int(ts)).hour
        day=  datetime.datetime.fromtimestamp(int(ts)).day

        month=  datetime.datetime.fromtimestamp(int(ts)).month
        year=datetime.datetime.fromtimestamp(int(ts)).year


        if hour < 12:
            savedata("data/", "Storage-Update-M-" +
                     str(day) + "-" +
                     str(month) + "-" +
                     str(year) + ".txt", datastr)
        else:
            savedata("data/", "Storage-Update-E-" +
                     str(day) + "-" +
                     str(month) + "-" +
                     str(year) + ".txt", datastr)


def parse(text):
    global currser
    global currtype
    global used
    global total
    global available
    global ts

    content = text.split("\n")

    for line in content:
        line = line[:len(line)]

        if line.__contains__("Netapp Cluster"):
            for server in servers:

                if line.__contains__(server):
                    currser = server
                    for type in types:
                        if line.__contains__(type):
                            currtype = type


                            # print(line)

        if line.__contains__("Total available capacity"):
            # print(line)

            # print ("contains","Total available capacity------")

            elements = line.split(":")
            # print (elements)
            size = float(elements[1])
            # print(size)
            total[currser + currtype] += size
            # print(size,"TOTAL capacity",total)

        elif line.__contains__("size provisioned"):
            # print(line)

            # print("contains", "Total   LUN    size  provisioned------- ")
            elements = line.split(":")
            # print(elements)
            size = float(elements[1])
            # print(size)
            used[currser + currtype] += size
            # print(size, "Used", used)

    # print( currser)
    # print( currtype)
    # print(    used)
    # print(total)
    # print(available)
    return (used, total)


def make_dir(dir_name):
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)


def main():
    slack_token = ""
    channel_name = ''
    time_on_last_message = time.time()
    channel_id = ""
    ts = 0.000
    threshmins = 20
    channels_call = SlackClient(slack_token).api_call("channels.list")
    print(channels_call)

    print(channels_call.keys())

    for channel in channels_call["channels"]:
        if channel["name"] == channel_name:
            channel_id = channel["id"]
            print(channel)

    make_dir(dir_name)
    print(channel_id)
    reset_data()
    time_since_last_update = time.time() - time_on_last_message
    print("Waiting for new data....", time.time() - time_on_last_message)
    if time_since_last_update > threshmins * 60:
        write_data(ts)

        reset_data()

    sc = SlackClient(slack_token)

    date_after_month = datetime.datetime.now() + relativedelta(months=-6)
    date_after_month=date_after_month.timestamp()
    while True:
        breakflag=0
        data = sc.api_call(
            "channels.history",
            channel=channel_id,
            oldest=date_after_month,
            count=1000,
        )

        if (data['ok'] == True):
            messages = data['messages']

            for message in reversed(messages):
                # print(message['ts'])
                if float(message['ts']) > ts:
                    print("difference=", float(message['ts']) - ts)
                    if float(message['ts']) - ts > (threshmins * 60):
                        print("greater diffrrece>reset................")
                        write_data(ts)
                        print(ts)

                        reset_data()

                    time_on_last_message = time.time()

                    ts = float(message['ts'])

                    parse(message['text'])

                if (data["has_more"] == True):
                    print("has more")
                    date_after_month=message['ts']
                else:
                    breakflag=1


        else:
            print("No data returned or error")

        time.sleep(1)  # in Seconds
        if(breakflag==1):
            break


main()

2 个答案:

答案 0 :(得分:0)

根据错误消息,elements[1]为空。并且Python无法将空字符串转换为float:

>>> float("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 

答案 1 :(得分:0)

elements[1]元素是一个无法解析为浮点数的字符串。最简单的方法是附加调试器并调查正在解析的内容。然后更改代码以更好地解析它。

第二种最简单的方法是二进制搜索使其失败的记录并修复您的代码以更好地解析它。

完全绝对首选的方法是,当您发现代码不支持的情况时,您会编写一个测试来证明该案例已被添加:

def test_parse_xyz():
    assert [("blablabla", None)] == parse(["blablabla: -certainly_not_a_float"])

这些测试可以通过例如自动检测。 pytest

$ pytest parser.py