jojo
jojo

Reputation: 41

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

Upvotes: 0

Views: 945

Answers (1)

bashBedlam
bashBedlam

Reputation: 1500

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 ('\n').split (',')
    print (f'{float (line_list [0])}   {float (line_list [1]):.5f}')

Upvotes: 1

Related Questions