Reputation: 53
I generated 2 columns in python, I transformed them in an np array, and managed to save them in a new file.
fi = np.array([co,pred[40]])
fi=fi.T
np.savetxt("Pred_40.dat", fi, delimiter=" ")
now I want to create an internal function that reads my new file. which I did but I got an error telling me: could not convert string to float'.'
def open_vmodel(vmodel_file,n_lag):
list_ = []
index = []
vargplot = open(vmodel_file)
for i in range(n_lag):
dm = vargplot.readline()
list_.append(list(map(float,dm)))
result = pd.DataFrame(list_)
result.columns = ['Lag Distance','Variogram Value']
istart = 0
return pd.DataFrame(result)
vmodel_df = open_vmodel("Pred_40.dat",n_lag=1)
Upvotes: 0
Views: 1877
Reputation: 231385
With a sample of your file (from the comment):
In [218]: txt="""0.000000000000000000e+00 0.000000000000000000e+00
...: 0.000000000000000000e+00 0.000000000000000000e+00
...: 1.000000000000000000e+01 5.626957416534423828e-01"""
In [220]: txt = txt.splitlines()
In [221]: txt
Out[221]:
['0.000000000000000000e+00 0.000000000000000000e+00 ',
'0.000000000000000000e+00 0.000000000000000000e+00 ',
'1.000000000000000000e+01 5.626957416534423828e-01']
Your map iterates on all characters of a line:
In [222]: list(map(float, txt[0]))
Traceback (most recent call last):
File "<ipython-input-222-47473c6c14f7>", line 1, in <module>
list(map(float, txt[0]))
ValueError: could not convert string to float: '.'
In [223]: list(txt[0])[:10]
Out[223]: ['0', '.', '0', '0', '0', '0', '0', '0', '0', '0']
But if you first split the line on space, as recommended by https://stackoverflow.com/a/66678555/901925
In [224]: list(map(float, txt[0].split()))
Out[224]: [0.0, 0.0]
Or for several lines (I like list comprehension better than map):
In [225]: res = []
...: for dm in txt:
...: res.append([float(i) for i in dm.split()])
...:
In [226]: res
Out[226]: [[0.0, 0.0], [0.0, 0.0], [10.0, 0.5626957416534424]]
Upvotes: 0
Reputation: 65
I'm pretty sure the line list(map(float, dm))
is wrong. It tries to parse each character of dm as a number, so when it tries to parse '.' as a number it fails. Maybe try something like list(map(float, dm.split()))
Upvotes: 2
Reputation: 65
Maybe it's a problem with localization, as some regions use comma instead of dot for decimal places. When you are casting to float, one of the numbers might be like 0.2
and Python might expect 0,2
.
import locale
locale.setlocale(locale.LC_NUMERIC,"C")
This will set the number format to the standard one, which will accept 0.5
instead of 0,5
Upvotes: 0
Reputation: 50
Can you use dm = vargplot.readline().replace(".","")
? There is a dot, so maybe it will fix your problem.
Upvotes: 0