python - How can make text output easy and shortly? -


i want modify text file. script long.

how can make short? can advise script.

i want made loop acap = line[4] ucap = line[5] ucp = line[6] saved = line[8] value tcap.

below script.

with open("find_c_volume_show.txt", "r") f:     line in f:         if "%" in line:             line = line.split(',')             vser = line[0]             vol = line[1]             aggr = line[2]             node = line[7]              if 'tb' in line[3]:                 tcap = str(line[3].split("tb")[0])                 tcap = float(tcap) if '.' in tcap else int(tcap)             elif 'gb' in line[3]:                 tcap = str(line[3].split("gb")[0])                 tcap = (float(tcap) if '.' in tcap else int(tcap) /1024)             elif 'mb' in line[3]:                 tcap = str(line[3].split("mb")[0])                 tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024)             elif 'kb' in line[3]:                 tcap = str(line[3].split("kb")[0])                 tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024 /1024)             elif 'b' in line[3]:                 tcap = str(line[3].split("b")[0])                 tcap = (float(tcap) if '.' in tcap else int(tcap) /1024 /1024 /1024 /1024)             else:                 print ("please check script.")              if 'tb' in line[4]:                 acap = str(line[4].split("tb")[0])                 acap = float(acap) if '.' in acap else int(tcap)             elif 'gb' in line[4]:                 acap = str(line[4].split("gb")[0])                 acap = (float(acap) if '.' in acap else int(tcap) /1024)             elif 'mb' in line[4]:                 acap = str(line[4].split("mb")[0])                 acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024)             elif 'kb' in line[4]:                 acap = str(line[4].split("kb")[0])                 acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024 /1024)             elif 'b' in line[4]:                 acap = str(line[4].split("b")[0])                 acap = (float(acap) if '.' in acap else int(tcap) /1024 /1024 /1024 /1024)             else:                 print ("please check script.")              if 'tb' in line[5]:                 ucap = str(line[5].split("tb")[0])             .....              if 'tb' in line[6]:                 ucp = str(line[6].split("tb")[0])              .....              if 'tb' in line[8]:                 saved = str(line[8].split("tb")[0])              .....             line = vser, vol, aggr, tcap, acap, ucap, ucp, saved     print (line) 

i want match tb capacity format in text. below text file.

vserver,volume,aggregate,total,avail,used,usedp,node,saved fas8040-zzzz,vol0,node1_aggr0,466.6gb,436.0kb,30.60mb,6%,fas8040-zzzz,0b fas8040-yyyy,vol0,node2_aggr0,46.6gb,428.6kb,38.07b,8%,fas8040-yyyy,23kb  fas8040-xxxx,vol0,node1_aggr0,466gb,436.0gb,30.60gb,6%,fas8040-xxxx,44gb fas8040-aaaa,vol0,node2_aggr0,466.6tb,428.6mb,38.07mb,8%,fas8040-aaaa,102mb 

maybe started:

unit = ['tb', 'gb', 'mb', 'kb', 'b'] open("find_c_volume_show.txt", "r") f:     line in f:         if "%" in line:            line = line.split(',')            out = []            l in line[3:6] + [line[8]]:              try:                ind = [unit[i] in l in range(5)].index(true)              except valueerror:                print('please check script')               val = str(l.split(unit[ind])[0])              out.append(float(val)/1024**ind)             line = line[0:3] + out[:-1] + line[6:8] + [out[-1]]            print(line) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -