Thank you all. Here is the last piece of code that caused me so much troubles but now working the way I wanted it:
fRawData = [] with open(fStagingFile2) as fStagingFile2FH: fRawData = [line.strip() for line in fStagingFile2FH.readlines()] # #### This is to read each element from the file and chop off the end of line character fNumberOfColumns = 7 fNumberOfRows = len(fRawData)/fNumberOfColumns fRowID = 0 fParameters = [] for fRowID in range(0, len(fRawData), fNumberOfColumns): fParameters.append(fRawData[fRowID:fRowID+fNumberOfColumns]) # #### This is to convert 1D array to 2D # #### ... and down below section is an example of how to read each element of the list # #### and how to update it if I need so. That was also a problem before. fRowID = 0 fColumnID = 0 for fRowID in range(fNumberOfRows): for fColumnID in range(fNumberOfColumns): if fColumnID == 0: fParameters[fRowID][fColumnID] = "XXXX" Message2Log("fParameters[" + str(fRowID) + "][" + str(fColumnID) + "] = " + str(fParameters[fRowID][fColumnID])) CU On Saturday, November 21, 2015 at 4:52:35 PM UTC+1, Nathan Hilterbrand wrote: > On 11/21/2015 10:26 AM, BartC wrote: > > On 21/11/2015 10:41, vostrus...@gmail.com wrote: > >> Hi, > >> I have a file with one parameter per line: > >> a1 > >> b1 > >> c1 > >> a2 > >> b2 > >> c2 > >> a3 > >> b3 > >> c3 > >> ... > >> The parameters are lines of characters (not numbers) > >> > >> I need to load it to 2D array for further manipulations. > >> So far I managed to upload this file into 1D array: > >> > >> ParametersRaw = [] > >> with open(file1) as fh: > >> ParametersRaw = fh.readlines() > >> fh.close() > > > > I tried this code based on yours: > > > > with open("input") as fh: > > lines=fh.readlines() > > > > rows = len(lines)//3 > > > > params=[] > > index=0 > > > > for row in range(rows): > > params.append([lines[index],lines[index+1],lines[index+2]]) > > index += 3 > > > > for row in range(rows): > > print (row,":",params[row]) > > > > For the exact input you gave, it produced this output: > > > > 0 : ['a1\n', 'b1\n', 'c1\n'] > > 1 : ['a2\n', 'b2\n', 'c2\n'] > > 2 : ['a3\n', 'b3\n', 'c3\n'] > > > > Probably you'd want to get rid of those \n characters. (I don't know > > how off-hand as I'm not often write in Python.) > > > > The last bit could also be written: > > > > for param in params: > > print (params) > > > > but I needed the row index. > > > To get rid of the '\n' (lineend) characters: > > with open(file1) as fh: > ParametersRaw = [line.strip() for line in fh.readlines()] > > or, more succinctly.. > > with open(file1) as fh: > ParametersRaw = [line.strip() for line in fh] > > Comprehensions are your friend. > > Nathan -- https://mail.python.org/mailman/listinfo/python-list