I'm just starting with python and I have a basic question about creating tables.
I have a file called example.txt as follow:
# Header information 1
# Header information 2
# Header information 3
# Header information 4
# var1 var2 var3 var4 var5
# Header information 5
# Header information 6
1 6500.0 5.052300 1.020020 9.355021
2 2500.7 3.038809 0.033637 1.736393
3 750.6 5.003003 0.047254 3.287092
...
...
...
100 2700.6 9.002800 0.088105 8.289851
I want to create a table using the name of the variables (var1, var2, etc.) as header names. I have tried this:
fnExample = open('example.txt', 'r')
i = 0
for line in fnExample:
i = i + 1
if i==5 :
varExample = [word.strip() for word in line.split()]
if (i>7 and i<100):
dataExample = [word.strip() for word in line.split()]
datatable[varExample[1]]=dataExample[0]
datatable[varExample[2]]=dataExample[1]
datatable[varExample[3]]=dataExample[2]
datatable[varExample[4]]=dataExample[3]
datatable[varExample[5]]=dataExample[4]
print(datatable['var1']) # gives only the last value of var1
Why does my datatable only contain the last value, and how can I fix it?