Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty importing .dat file

Tags:

python

pandas

I am somehow having difficulty reading in this file into python with pandas read_table function. http://www.ssc.wisc.edu/~bhansen/econometrics/invest.dat

This is my code:

pd.read_table(f,skiprows=[0], sep="")

Which yields error:

TypeError: ord() expected a character, but string of length 0 found
like image 817
zsljulius Avatar asked Dec 11 '14 01:12

zsljulius


People also ask

Why can't I open a DAT file?

If the DAT file is inside a system folder, you shouldn't attempt to open it, because it could be in use by one of your apps as a configuration file. You can also use the trial-and-error method by trying to open it with several apps, or you can contact the creator of the file.

How do I make a DAT file readable?

dat file in Windows using a text editor, right-click on the file you want to open, and select Open With. Select the text editor you want to use, and click OK. You'll be able to read the file's contents if it's a text-based . dat file.

What app should I use to open .DAT files?

dat Explorer is a free app to open those 'winmail. dat' attachments. This app is free of charge, giving you access to the original attachment files without any need for further in-app purchases. The optional in-app-purchase will remove ads and help fund further development of new features.


1 Answers

Dont know about read_table, but you can read this file directly as follows:

import pandas as pd    

with open('/tmp/invest.dat','r') as f:
    next(f) # skip first row
    df = pd.DataFrame(l.rstrip().split() for l in f)

print(df)

Prints:

              0            1             2            3
0     17.749000   0.66007000    0.15122000   0.33150000
1     3.9480000   0.52889000    0.11523000   0.56233000
2     14.810000    3.7480300    0.57099000   0.12111000
...
...

The same can be obtained as follows:

df = pd.read_csv('/tmp/invest.dat', sep='\s+', header=None, skiprows=1)
like image 107
Marcin Avatar answered Oct 11 '22 12:10

Marcin