How can I read a text file and save the numbers inside a group to a 5 different lists in python -
i have text file have structure:
transactions/sec group = aa\code1\kb 100 200 300 400 500 transactions/sec group = ab\code2\kb 800 300 400 500 600 transactions/sec group = ac\code3\kb 400 300 500 600 700 transactions/sec group = ad\code4\kb 200 200 300 400 400
i read each group , save 5 numbers in 5 different lists. example, read group 1, , create list 1, list 2, list 3, list 4, list 5. read group 2, , numbers list 2, "append" them in list 1, list 2, list 3, list 4, list 5...
this did:
with open('output.txt') foutput: count = 0 list1 = [] list2 = [] list3 = [] list4 = [] list5 = [] line in foutput: #print line count = count + 1 if 'group' in line: key = line[line.index('=')+1:].strip() #f.write(key) else: value = float(line.strip()) list1 = value ind += 1 list2 = float(line.strip() + '\n')
the code not working, have 5 numbers in 1 list, , need each number in different lists....
it doesnt give me error having numbers in 1 list....(or in 1 variable, value variable)
thank help.
if can assume file constant format every time, can this. i'm going use dict instead of 5 lists, , use item after = key.
f = open("output.txt", "r") lines = [line.rstrip() line in f if line != "\n"] #get lines, strip newline chars, , remove lines newlines. lines.close() num_lists = 5 groups = [[] in range(num_lists)] listindex = 0 line in lines: if "transactions/sec group" not in line: groups[listindex].append(float(line)) listindex += 1 if listindex == num_lists: listindex = 0
Comments
Post a Comment