开发者

Reading ASCII file in Python (numpy-array?)

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?


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.


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

Examples of csv usage

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜