Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

python - How to import .DAT file containing D as scientific notation instead of E into Numpy?

Dear Developers/Users!

I have .DAT files generated from LMGC90 simulation code. All .DAT files contain the columns with D as scientific notation instead of E. I tried a lot but failed to import into numpy for plotting. Is there any method to deal with these data?

Thank you very much for help!

Best regards

Ram

question from:https://stackoverflow.com/questions/65944800/how-to-import-dat-file-containing-d-as-scientific-notation-instead-of-e-into-nu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...