You might need the following code. You will probably need the re module.
You can open the file for reading using:
f = open("file_name_here")
You can read the file one line at a time using
line = f.readline()
To go to the next line starting with "#", you can use:
while not line.startswith("#"): line = f.readline()
To parse a string that looks like "# i j", you can use the following regular expression:
is_match = re.match("#\s+(\d+)\s+(\d+)",line) if is_match: i = is_match.group(1) j = is_match.group(2)
See the documentation for the re module for more information.
To parse a block, you can use the following bit of code:
block = [[]] # block[i][j] will contain element i,j in your block while not line.isspace(): # read until next blank line block.append(map(float,line.split(" "))) # splits each line at each space and turns all elements to float line = f.readline()
Then you can turn your block into a numpy array:
block = np.array(block)
If you imported numpy as np. If you want to read several blocks between i and j, just put the above code to read one block into a function and use it several times.
Hope this helps!
source share