FlameDra
FlameDra

Reputation: 2087

Get filename and date time from string

I have file names in the following format: name_2016_04_16.txt

I'm working with python3 and I would like to extract two things from this file. The prefix, or the name value as a string and the date as a DateTime value for the date represented in the string. For the example above, I would like to extract:

filename: name as a String

date: 04/16/2016 as a DateTime

I will be saving these values to a database, so I would like the DateTime variable to be sql friendly.

Is there a library that can help me do this? Or is there a simple way of going about this?

I tried the following as suggested:

filename = os.path.splitext(filename)[0]
        print(filename)
        filename.split("_")[1::]
        print(filename)
        '/'.join(filename.split("_")[1::])
        print(filename)

But it outputs:

name_2016_04_16  
name_2016_04_16  
name_2016_04_16

And does not really extract the name and date.

Thank you!

Upvotes: 0

Views: 7784

Answers (3)

Rossen Dimitrov
Rossen Dimitrov

Reputation: 1

You can split the filename on "." Then split again on "_". This should give you a list of strings. The first being the name, second through fourth being the year, month and day respectively. Then convert the date to SQL friendly form.

Something like this:

rawname ="name_2016_04_16.txt"
filename = rawname.split(".")[0] #drop the .txt
name = filename.split("_")[0] #provided there's no underscore in the name part of the filename
year = filename.split("_")[1]
month = filename.split("_")[2]
day = filename.split("_")[3]
datestring = (month,day,year) # temp string to store a the tuple in the required order 
date = "/".join(datestring) #as shown above
datestring = (year,month,day)
SQL_date = "-".join(datestring ) # SQL date
print name
print date
print SQL_date

Unless you want to use the datetime library to get the datetime date, in which case look up the datetime library You can then do something like this:

SQL_date = datetime.strptime(date, '%m/%d/%Y')

This is the most explicit way I can think of right now. I'm sure there are shorter ways :)

Apologies for the bad formatting, I'm posting on mobile.

Upvotes: 0

Lloyd Kirk
Lloyd Kirk

Reputation: 175

I would first strip the file extension, then I would split by underscore, removing the 'name' field. Finally, I would join by slash (maybe this value could be logged) and parse the date with the datetime library

import os
from datetime import datetime

file_name = os.path.splitext("name_2016_04_16.txt")[0]
date_string = '/'.join(file_name.split("_")[1::])
parsed_date = datetime.strptime(date_string, "%Y/%m/%d")

To make the date string sql friendly, I found this so post: Inserting a Python datetime.datetime object into MySQL, which suggests that the following should work

sql_friendly_string = parsed_date.strftime('%Y-%m-%d %H:%M:%S')

Upvotes: 2

durbachit
durbachit

Reputation: 4886

How about simply doing this?

filename = 'name_2016_04_16.txt'
date = filename[-14:-4] # counting from the end will ensure that you extract the date no matter what the "name" is and how long it is
prefix = filename [:-14]

from datetime import datetime

date = datetime.strptime(date, '%Y_%m_%d') # this turns the string into a datetime object

(However, this works on Python 2.7, if it works for Python 3 you need to find for yourself.)

Upvotes: 1

Related Questions