将字符串分割成给定大小的组 [英] split string into groups of given size
问题描述
time =165420#16:54:20(或,4:54 PM和20秒)
我想要一个脚本,可以将时间戳转换成日期-时间。我有一年,一个月,一天已经没有问题。但是对于时间的局面,我绝望地找到一个优雅简单的解决方案。我决定先强制它继续执行我的代码,最后是:
import datetime
/ pre>
dt_timestamp = extract_dt(2012-11-09-165420)#example
def extract_dt(timestamp):
ds = timestamp.split(' - ')#split into组(0,1,2是日期,3是时间)
d = [int(i)for ds in [ds] [] 3]] [int(ds [3] [0:2]), int(ds [3] [2:4]),int(ds [3] [4:6])]
return datetime.datetime(* d)
在尝试解决我的情况时,我看到很多变体,有很多答案。
我发现此解决方案 :
def nsplit(s,n):
return [s [k:k + n] for k in xrange(0,len(s),n)]
哪个对我很好:
>>> s =165420
>>> n = 2
>>> [s] [k:k + n] for x in xrange(0,len(s),n)]
['16','54','20']
但我确信,必须将一个更好,更优雅的方式将字符串分割成相同大小的组,如<我提供了一个href =http://bytes.com/topic/python/answers/157385-splitting-string-into-groups-three-characters =nofollow>链接(但是由于链接海报有一个错字):
TheFunction(Hello World,3)
返回:
['Hel','lo','Wor','ld']
如果可能的话,我更喜欢它不是一个功能,我想要一个在线的方法。它应该尽可能高效。我总是可以从我发现的函数中提取列表的理解,但是我觉得它的效率不是很高,而且看起来并不正确。
答案可能是很简单,我只是不知道。再次感谢
解决方案我不知道比您在问题中提到的更优雅的分块字符串解决方案,但是
datetime.datetime.strptime()
解决了您的实际问题:>>> from datetime import datetime
>>>> datetime.strptime(2012-11-09-165420,%Y-%m-%d-%H%M%S)
datetime.datetime(2012,11,9,16,54, 20)
My situation is I have a list of time-stamped items, but the actual time-of-day is all squished together. Like this:
time = "165420" # 16:54:20 (or, 4:54 PM and 20 seconds.)
I wanted a script to go over the lines and be able to convert the time-stamp into a date-time. I have the year, month, day already that was no problem. But for the time situation I was desperate to find an elegant and simple solution. I decided to brute force it at first to continue with my code, and ended up with:
import datetime dt_timestamp = extract_dt("2012-11-09-165420") # example def extract_dt(timestamp): ds = timestamp.split('-') #split into groups (0,1,2 are the date, 3 is the time. d = [int(i) for i in ds[:3]] + [int(ds[3][0:2]), int(ds[3][2:4]), int(ds[3][4:6])] return datetime.datetime(*d)
While trying to solve my situation I've seen many variations on this question, with many answers.
I have found this solution:
def nsplit(s, n): return [s[k:k+n] for k in xrange(0, len(s), n)]
Which works nicely for me:
>>> s = "165420" >>> n = 2 >>> [s[k:k+n] for k in xrange(0, len(s), n)] ['16', '54', '20']
But I am pretty sure there MUST be a better, more elegant way of splitting a string into groups of equal size, as was mentioned in the link I provided (but fixed because the link poster had a typo):
TheFunction("Hello World",3)
Returns:
['Hel','lo ','Wor','ld']
I would prefer, if possible, that it not be a function, I want a way to do it in-line. And it should be as efficient as possible. I can always extract the list-comprehension from the function I found, but I have a feeling its not very efficient, and it just doesn't look right.
The answer is probably something very simple I just didn't know about. Thanks again.
解决方案I don't know of a more elegant solution to chunking strings than those you mention in your question, but
datetime.datetime.strptime()
solves your practical problem:>>> from datetime import datetime >>> datetime.strptime("2012-11-09-165420", "%Y-%m-%d-%H%M%S") datetime.datetime(2012, 11, 9, 16, 54, 20)
这篇关于将字符串分割成给定大小的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!