Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How can I load a .dat file (that is comma separated) in python?

I am trying to load a data file that is two columns of data, separated by a comma using: Data = np.loadtxt('real3kHz.dat'), where real3kHz.dat is the name of the file with the following data:

4998.29641,-0.00003
4997.33205,-0.00005
4996.36768,-0.00007
4995.40332,-0.00008
4994.43895,-0.00008
4993.47459,-0.00008
4992.51023,-0.00006
4991.54586,-0.00006
4990.5815,-0.00005
4989.61714,-0.00006
 ....

However, I keep getting the following error:

ValueError: could not convert string to float: '4998.29641,-0.00003'

I presumed that I successfully changed the cvs file into a .dat file (by saving it as a .dat file in notepad) before loading it into python. Can I get some help understanding where I am going wrong? Thank you in advance

question from:https://stackoverflow.com/questions/65837457/how-can-i-load-a-dat-file-that-is-comma-separated-in-python

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

1 Answer

0 votes
by (71.8m points)

Try this and see if it's doing what your asking for :

with open ('real3kHz.dat') as data_file :
for line in data_file :
    line_list = line.strip ('
').split (',')
    print (f'{float (line_list [0])}   {float (line_list [1]):.5f}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...