0

I have .dat file that I want to use in my script which draws scatter graph with data input from that .dat file. I have been manually converting .dat files to .csv for this purpose but I find it not satisfactory. This is what I am using currently.

import pandas as pd import matplotlib.pyplot as plt import numpy as np

filename=raw_input('Enter filename ')
csv = pd.read_csv(filename)
data=csv[['deformation','stress']]
data=data.astype(float)
x=data['deformation']
y=data['stress']
plt.scatter(x,y,s=0.5)

fit=np.polyfit(x,y,15) 
p=np.poly1d(fit)
plt.plot(x,p(x),"r--")
plt.show()

Programmer friend told me that it would be more convenient to convert it to JSON and use it as such. How would I go about this?

3
  • 2
    look at stackoverflow.com/questions/38082532/read-dat-file-using-python Commented Oct 11, 2017 at 12:56
  • 2
    What is a dat file? I dont think there is any standard for that? Next, what is the problem of your current approach? I don't think converting to JSON is of any help at all. But one can only judge on that if the input data format is known . Commented Oct 11, 2017 at 13:01
  • .dat file looks as such: "deformation","stress" 1.26449846,124 1.27635146,137 ... Commented Oct 12, 2017 at 7:59

1 Answer 1

0

try using the numpy read feature

import numpy as np

yourArray = np.fromfile('YourData.dat',dtype=dtype)

yourArray = np.loadtxt('YourData.dat')

loadtxt is more flexible than fromfile

Sign up to request clarification or add additional context in comments.

1 Comment

If you assume this to be working, then pd.read_csv("data.dat", ...) would work as well... in which case there wouldn't actually be any problem in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.