join - How can work output on python? -
i want made script storage capacity modify have problem.
my script has below error:
traceback (most recent call last): file "test.py", line 20, in <module> mod_f.write(','.join(line) + '\n') typeerror: sequence item 3: expected string, float found
below script.
mod_f = open("mod_c_vol_size.txt", 'w') mod_f.write("vserver,volume,aggregate,total,avail,node,savea,saved,savec,snap,tused\n") unit = ['tb', 'gb', 'mb', 'kb', 'b'] open("find_c_vol_modify.txt", "r") f: next(f) line in f: if len(line.strip()) != 0: line = line.split(',') out = [] l in line[3:5] + line[6:11]: l = l.replace('\n','') 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[:-5] + line[5:6] + out[2:] print (line) mod_f.write(','.join(line) + '\n') mod_f.close()
error @ line mod_f.write(','.join(line) + '\n')
below input text file.
vserver,volume,aggregate,total,avail,node,savea,saved,savec,snap,tused xxxx,yyyy,node1_aggr1,1tb,1023gb,fas8040-zzzz,0b,0b,0b,0b,177.7mb xxxx,zzzz,node1_aggr1,3tb,3.00tb,fas8040-zzzz,0b,0b,0b,0b,1.60gb xxxx,cccc,node1_aggr1,1tb,907.9gb,fas8040-zzzz,0b,0b,0b,0b,116.1gb xxxx,vvvv,node1_aggr1,200gb,200.0gb,fas8040-zzzz,0b,0b,0b,0b,6.25mb
below expected output file.
['xxxx', 'yyyy', 'node1_aggr1', 1.0, 0.9990234375, 'fas8040-zzzz', 0.0, 0.0, 0.0, 0.0, 0.00016946792602539061] ['xxxx', 'zzzz', 'node1_aggr1', 3.0, 3.0, 'fas8040-zzzz', 0.0, 0.0, 0.0, 0.0, 0.0015625] ['xxxx', 'cccc', 'node1_aggr1', 1.0, 0.88662109375, 'fas8040-zzzz', 0.0, 0.0, 0.0, 0.0, 0.11337890625] ['xxxx', 'vvvv', 'node1_aggr1', 0.1953125, 0.1953125, 'fas8040-zzzz', 0.0, 0.0, 0.0, 0.0, 5.9604644775390625e-06]
the fourth item in each line
list float
element computed: 1.0, 3.0, 1.0, 0.1953125
(as later elements). join
works on strings. you'll need convert float
items string
somewhere. perhaps
out.append(str(float(val)/1024**ind))
or generality before write
line = [str(item) item in line]
Comments
Post a Comment