Admittedly, this will be a very naive question, but I'm really curious to learn how one can achieve the following in a simple manner:
Suppose we have a text file formatted like so:
#name input
#x = 500
#y = 150
20 53.2 60 20.1 100 200
2 93.3 65 22.2 400 300
.... (continues with more data similarly)
So all headers are on top of the file, and the numerical data is a mix of integers and floats, and in this example we have 6 columns.
I'm trying to learn how one can in a straightforward manner extract each column and map to a variable, moreover, to also extract certain info from the header, for instance we want to know what x is (so we want to extract 500 from the header).
The reason I ask for simple approaches is because most solutions to these kinds of questions I've come across here on SE are quite compact and hard to decipher for newcomers, e.g. here.
If I were to do this task in Python, my approach would go as follows:
import numpy as np
fname = 'sample'
data = np.genfromtxt(fname,comments='#')
firstcol = data[:,0]
secondcol = data[:,1] #and similarly for other columns
x = 0
#now we find where the header starting with #x is and save its value in x:
with open(fname,'r') as f:
lines = f.readlines()
for el in lines:
if el.startswith('#x'):
x = int(el[el.rindex(' ')+1:len(el)])
- In principle, would there be the possibility of going about this task in Mathematica similar to the above approach? I admit it's not the most elegant approach, but I would learn a lot by the direct comparison of the approaches.
~~ ___] & /@ whole /.seems really contrived, is there a slower way of going about it? So one sees what each step is doing. Admittedly, these advanced syntax notations in Mathematica are still unfamiliar to me. – Apr 24 '19 at 06:35