2

I have a problem reading ASCII file in Python. Here's an example of the file: http://pastebin.com/CTwwPPKA

I tried using numpy's genfromtxt:

data = np.genfromtxt("example.txt")

But this way I cannot read dates and times properly since they should be datetime objects. On the other hand, loadtxt can only read float values, which is also not acceptable.

Could you please suggest me a way to properly read that kind of file?

0

2 Answers 2

3

you have to use dtype option here.

x = np.genfromtxt("example.txt", dtype=None)
print(x[0])

and you will get

('DATA', 34967565, '2011-08-04', '19:00:00:081', 0.0272448, -0.17718500000000001, 4.2143899999999999, 524.57600000000002, 17.485499999999998, 101.07599999999999, 0.45927400000000002, 0.19031300000000001, 0.100296, 0.97492599999999996, 1.94354, 100.73399999999999, 12.538600000000001, 10.3786, 44318.5, 39605.5, 39234.5, 40298.0, 68)

The trick here is that you have to specify dtype to None so that numpy can automatically recognize strings and numbers, while the default dtype is float.

Then you can use datetime.strptime to convert the strings to datetime objects accordingly.

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

Comments

3

You want to use csv.reader() with the csv.excel_tab dialect.

Examples of csv usage

2 Comments

The problem is that the file has both tab and space delimiters.
Not according to what you pasted.

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.