1

I'm new to python.

I have a program that reads from str(sys.argv[1]):

myprogram.py "some date" # I'd like this in YYYYMMDD format. I.e:
myprogram.py 20160806


if __name__ == '__main__':
    if str(sys.argv[1]):
        CTRL = str(sys.argv[1])
        print "some stuff"
        sys.exit()

I need "some date" in YYYYMMDD format. How could it be possible? I've googled variable mask, variable pattern and nothing came out.

Thanks for your help and patience.


UPDATE:

Fortunately all answers helped me!

As the CTRL variable gaves me 2016-08-17 00:00:00 format, I had to convert it to 20160817. Here is the code that worked for me:

if str(sys.argv[1]):
    CTRL_args = str(sys.argv[1])

    try:
        CTRL = str(datetime.datetime.strptime(CTRL_args, "%Y%m%d")).strip().split(" ")[0].replace("-","").replace(" ","").replace(":","")
        # do some stuff
    except ValueError:
        print('Wrong format!')
        sys.exit()
6
  • 1
    'I need "some date" in YYYYMMDD format'. What is "some date"? It doesn't appear anywhere in your code. Commented Aug 18, 2016 at 14:18
  • @Kevin I've edited. It's that clear? Commented Aug 18, 2016 at 14:21
  • Again one of my questions downvoted? What's wrong? Commented Aug 18, 2016 at 14:24
  • It's a little clearer, but "20160806" is already in YYYYMMDD format. What kind of output are you trying to produce? Should your program say "that input is in YYYYMMDD format, great job"? If the user enters "myprogram.py spamAndEggs", should it say "that's not a YYYYMMDD date, try again"? Or what? I still don't know what you're actually trying to do. Commented Aug 18, 2016 at 14:32
  • it should it say "that's not a YYYYMMDD date. Commented Aug 18, 2016 at 14:34

2 Answers 2

2

you need function datetime.strptime with mask %Y%m%d

import sys
from datetime import datetime

if __name__ == '__main__':
    if str(sys.argv[1]):
        CTRL = str(sys.argv[1])
        try:
            print datetime.strptime(CTRL, "%Y%m%d")
        except ValueError:
            print 'Wrong format'
        sys.exit()

Output:

$ python example.py 20160817
2016-08-17 00:00:00
Sign up to request clarification or add additional context in comments.

2 Comments

How could I put this on the ifstatement to ensure it's not entered in different format?
I added validation to original answer
0

If I understand what you said right, you want to pass an argument in a YYYYMMDD format.

There is nothing stopping you from doing this with your script.

You can run: python yourscript.py YYYYMMDD and a string "YYYYMMDD" will be stored in your CTRL variable.


UPDATE:

The following routine does the checks you are asking for:

Let me know if you need me to explain any of it!

import sys
from datetime import datetime

if __name__ == '__main__':
    try:
        CTRL = str(sys.argv[1])
    except IndexError:
        print "You have not specified a date!"
        sys.exit()

    try:
        parced_CTRL = datetime.strptime(CTRL, "%Y%m%d")
    except ValueError:
        print "Please input date in the YYYYMMDD format!"
        sys.exit()

    print "Date is in the correct format!"
    print "Data = {}".format(parced_CTRL)

UPDATE:

If you want to parse dates in the YYYY-MM-DD format you need to do this instead:

parced_CTRL = datetime.strptime(CTRL, "%Y-%m-%d")

I think it is self explanatory... ;)

5 Comments

that's exactly it does. But the parameter should be in YYYYMMDD format, nothing different that that
so you want to check if the format is correct? and if it is not, you want and "error message" output?
If the format is in YYYYMMDD format do other stuff otherwise print the error message. I haven't wrote that in my question to keep it simple.
Is this what you were looking for?
It worked! But I had to convert the string 2016-08-17 00:00:00 to 20160817 using CTRL = str(datetime.datetime.strptime(parced_CTRL, "%Y%m%d")).strip().split(" ")[0].replace("-","").replace(" ","").replace(":","")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.