Essentially, if I have a file:
"Field_20130515-212300"
I want to extract the date 20130515-212300
. I have a method, but it is inelegant. Just wondering if there's a better way to do this.
times = os.listdir("path\\to\\your\\file")
for idx, time in enumerate(times):
time[idx] = time[-15:]
This has been solved using list comprehension thanks to @alani.
times = os.listdir("path\\to\\your\\file")
times = [t.split("_")[1] for t in times]
Seems simple to do it in a one liner..
import os
times = [file[-15:] for file in os.listdir('path\\to\\your\\file')]
Or with split()
import os
times = [file.split('_')[1] for file in os.listdir('path\\to\\your\\file')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.