I use Python to import data from external files. Example file:
Things Thing1 Thing2 Thing3
Color -98564.0 -745871.0 -335430.0
Age 11.0 20.0 2.0
Weight 50.75 52.9 65.2
Volume 30.232 45.876 100.3
Each column is a thing. Each row is a different property (like color or age). The files all have the same number of rows in the same order. However, they can have different numbers of columns.
I know how to import and access the data I need for a specific file (i.e., where I specify the number of columns in the file). However, it would be great to be able to deal with any file (any number of columns). So far I have:
n_cols = 4
exampleFileImport = np.loadtxt("exampleFile.dat",usecols=np.arange(1, n_cols), skiprows=1)
for n in range(n_cols-1):
ColorImport = solidSlop98Data_rxn1[0,n]
AgeImport = solidSlop98Data_rxn1[1,n]
WeightImport = solidSlop98Data_rxn1[2,n]
VolumeImport = solidSlop98Data_rxn1[3,n]
My problem is that I don’t know how to come up with a unique variable name for n columns. I.e., whenever I go through the loop, the value of “ColorImport” (or whatever) is overwritten. I am trying to append a “_i
” (e.g., ColorImport_1
) but am getting confused. I know from a previous post here that I can append one string to another by doing:
str1 = "one"
str2 = "two"
newstr = " ".join((str1, str2))
where str1 would be (for example) “ColorImport” and i would be a column number, but am somehow not understanding how to apply it in my situation. If anyone would please point me in the right direction, I would be very grateful.