python - Assigning name to lines read from a file -


i reading file line line (32 lines in binary) storing in variable named regfile[i]. have assign name each line r0 in variable regfile[i]= 000000001000000000000 how can assign each line in regfile[i]?? here piece of code.

def regread(filename):        file = open (filename, 'r')       regline  = file.readlines()        reg= regline       reg= map(lambda s: s.strip(), reg)        in range(0,32):           regfile[i] = int(reg[i])               'r'i = regfile[i]                print 'r' ,i       file.close() 

i don't see why want name variable - index regfile... following pythonic way of writing have (using islice make sure @ 32 lines read , int(line, 2) convert binary integer)...

from itertools import islice  open('somefile') fin:     reg_file = [int(line, 2) line in islice(fin, 32)]  print reg_file[0], reg_file[31] 

as @mgilson points out - if wanted put them in dict:

reg_file = {'r{}'.format(idx):int(line, 2) idx, line in enumerate(islice(fin, 32))} 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -