2017-12-11 17 views
-2

データをスラックチャンネルからファイルに移行する際にエラーが発生しました。pythonエラーが文字列を浮動小数点に変換できませんでした

しかし、2か月のデータのスクリプトを実行すると、別々のファイルに10日間のデータが表示されますが、特定の日付にエラーが表示されます。たるみのソースデータは、我々はいくつかの値は、我々はこのために価値が得0多分誤りであることがわかった元データを1として期待

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: 

とは少し異なっている可能性があります。未来を飛ばしたり続ける方法はありますか? elements[1]が空のエラーメッセージに基づいて、

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() 
+0

'element [1]'の値を投稿できますか? – vmonteco

+1

エラーが示すように、 'elements [1]'は浮動小数点に変換できない文字列です。これは、あなたが提供した情報で言えることです。これが起こるとき、 'elements [1]'の値は何ですか? – Galen

+0

私はいくつかの場所で要素[1]が値 '0'を持っていることを確認しました。そのような値は最近のソースデータにもあります。そして、スクリプトを適切に実行することができます。 – Poo

答えて

0

。そして、Pythonは、空の文字列をfloat型に変換することはできません。

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

elements[1]要素がフロートに解析することはできません文字列です。最も簡単な方法は、デバッガをアタッチし、何が解析されているかを調べることです。その後、コードを変更して解析してください。

2番目の最も簡単な方法は、失敗したレコードをバイナリ検索してコードを修正してよりよく解析することです。

全く絶対に好ましい方法は、あなたがケースがあなたのコードがサポートされていなかったということであったものを見つけたとき、あなたはそのような場合は、追加されたことを証明するテストを書くでしょう、に次のようになります。

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

これらのテスト自動的に検出することができますpytest

$ pytest parser.py 
+0

生データ中のある値は、値が「0」である。しかし、どうすればこの問題を解決できますか? – Poo

+0

"0"を完全に解析して浮動させることができます。あなたは間違ったコーナーを見ている。これをどのようにデバッグしますか?エラーを再現する単体テストを作成してみませんか?私は多くの印刷ステートメントを見ています。おそらく、デバッガの使用方法を学ぶのに時間を費やすことで利益を得ることができます。 'pytest' – xtofl

+1

あなたのコメントに基づいて私はいくつかのデバッグを行い、いくつかの値が利用できないことを発見しました。 – Poo

関連する問題