Skip to content Skip to sidebar Skip to footer

Np.fromregex With String As Dtype

I have a file with dates formatted as '1:*? year mo da ho mi se.condsdec', (with '?' being a 1 character wildcard) ie: *A 2014 12 31 23 59 59.123456 I would like to extract this e

Solution 1:

Your problem is you are setting the dtype as int or float when you should be specifying them as np.str_. You also need to specify the length of the string so

import numpy as np

time_pattern=r'\*.{2}(\d{4}) (\d{2}) (\d{2}) (\d{2}) (\d{2}) (\d{2}\.\d{8})'
t_dtype=[('year',np.str_,4),('month',np.str_,2),('day',np.str_,2),\
('hour',np.str_,2),('min',np.str_,2),('sec',np.str_,3)]

out=np.fromregex('filename',time_pattern,t_dtype)
print(out)

If you look at the second example of this, it shows how to handle strings

Post a Comment for "Np.fromregex With String As Dtype"