Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks! That helped. – abudis Sep 21 '11 at 10:22

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

Examples of csv usage

share|improve this answer
    
The problem is that the file has both tab and space delimiters. – abudis Sep 21 '11 at 9:27
1  
Not according to what you pasted. – Ignacio Vazquez-Abrams Sep 21 '11 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.