Reputation: 195
I have a file input.dat with contents:
1.00000000 1.00000000 0.00000000 0.10000000 12 12 50000 2
I want to read these values and assign them to variables a1 to a8 respectively.
I try:
with open("input_params.dat","r") as inpfl:
a1,a2,a3,a4,a5,a6,a7,a8 = inpfl.read()
and get the error:
ValueError: too many values to unpack (expected 8)
How can I modify read() to do what I want?
I also need a1,..., a8 to be floats and integers respectively and not string variables. How do I accomplish this?
Upvotes: 0
Views: 828
Reputation: 327
you can just use below code:
f = open('1.txt' , 'r')
lines = f.read().split(" ")
so you have list of values and you can take it in variable if you want like below:
a = lines[0]
, b = lines[1]
and so on.
You can use a1,a2,a3 = f.read().split(" ")
if you want it in variables. Provided you have exact number of variables as values in file.
Hope this helps.
Upvotes: 3