You could use the loadtxt
function in numpy with a converter.
Sample data file data.DAT
1D-8
2D-7
3D-6
import numpy as np
data = np.loadtxt('data.DAT', converters={0: lambda s: s.replace(b'D', b'E')})
The 0
is defining the converter for the first column. If you had a bunch of columns like this you could define a function for the converter and then call it multiple times. Just make sure to use bytes instead of strings or a TypeError will be raised.
To use a function with multiple columns of data it might look like this.
def replace_d_exp(s):
return s.replace(b'D', b'E')
data = np.loadtxt('data.DAT', converters={
0: replace_d_exp,
1: replace_d_exp,
})
If all your data has this issue you could even just use a dictionary comprehension to define the converters.
def replace_d_exp(s):
return s.replace(b'D', b'E')
data = np.loadtxt('data.DAT', converters={n: replace_d_exp for n in range(2)})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…