stdout - How can work output using python? -
i made script modify text.
but can't make result. below script. begin study python.
i think script didn't work because f = open('find_c_volume_show.txt', 'w')
please me.
import sys itertools import islice def next_n_lines(file_opened, n): return [x.strip() x in islice(file_opened, n)] field_line = 1 num = 0 n = 9 split_line = field_line / n strings = ("vserver", "volume name", "used size", "available size", "volume size", "aggregate name", "space saved storage efficiency") f = open('find_c_volume_show.txt', 'w') line in open("c_volume_show.txt"): if any(s in line s in strings): field1,field2 = line.strip().split(':') field_line += 1 f.write(field2 + '\n') f.close() f = open('find_c_volume_show.txt', 'w') f.write("vserver,volume name,aggregate name,volume size,available size,used size,space saved\n") open('find_c_volume_show.txt', 'w') result: while num < split_line: num += 1 lines = next_n_lines(result, n) f.write('{}'.format(','.join(lines)) +'\n' ) f.close()
below text file. text file has data multi data.
vserver name: fas8040-zzzz volume name: vol0 aggregate name: node1_aggr0 volume size: 466.6gb available size: 436.2gb filesystem size: 466.6gb total user-visible size: 466.6gb used size: 30.40gb used percentage: 6% node name: fas8040-zzzz space saved storage efficiency: 0b percentage saved storage efficiency: 0% space saved deduplication: 0b space saved compression: 0b percentage space saved compression: 0% total physical used size: 22.37gb
i want result below.
vserver,volume name,aggregate name,volume size,available size,used size,space saved fas8040-zzzz,vol0,node1_aggr0,466.6gb,435.7gb,30.92gb,0b fas8040-yyyy,vol0,node2_aggr0,466.6gb,428.7gb,37.91gb,0b fas8040-xxxx,vol0,node2_aggr0,466.6gb,428.7gb,37.91gb,0b
the problem each time open file open(filename,'w')
, gets erased. can use "temporal" file different name store results of first for
loop, or suggest aggregate content of each line list, , write straight away.
additionally, have problems "split_line" value, 0. guess mean len(strings)
.
here code:
import sys strings = ("vserver", "volume name", "used size", "available size", "volume size", "aggregate name", "space saved storage efficiency") open('find_c_volume_show.txt', 'w') f: f.write("vserver,volume name,aggregate name,volume size,available size,used size,space saved,snapshot,total used size\n") row = [] line in open("c_volume_show.txt"): if any(s in line s in strings): field1,field2 = line.strip().split(':') row.append(field2) print(row) if len(row) == len(strings): f.write('{}'.format(','.join(row)) +'\n' ) print(row) row = []
Comments
Post a Comment