KJ9 KJ9 - 3 years ago 62
Python Question

How can make text output easy and shortly?

I want modify some text file. But My script too long.

How can make short? who can advise about my script.

I want made loop

acap = line[4] ucap = line[5] ucp = line[6] saved = line[8]
value like
tcap
.

below is my script.

with open("find_c_volume_show.txt", "r") as f:
for 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 to TB capacity format in text.
below is my 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

Answer Source

Maybe this will help get you started:

unit = ['TB', 'GB', 'MB', 'KB', 'B']
with open("find_c_volume_show.txt", "r") as f:
    for line in f:
        if "%" in line:
           line = line.split(',')
           out = []
           for l in line[3:6] + [line[8]]:
             try:
               ind = [unit[i] in l for i 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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download