Python Checking Date Format Failing to Work
My program takes in data in the form of:
LASTNAME|FIRSTNAME|GENDER|DOB
The first thing I do is use regular expressions to detect the delimiter and split the fields. I allow a space, comma, or pipe as a delimiter. I know which field is DOB and have printed it out to ensure I'm not dealing with the wrong field.
My try code is as follows:
try:
#check if the fields are good
fields = re.split(r'[ ,|]+', line)
except:
#if not good: put it on the failure list
flist.append(line.replace('n', ''))
LastName = fields[0]
FirstName = fields[1]
Gender = fields[2]
DOB = fields[3]
#one last try... make sure the DOB is good
try:
datetime.datetime.strptime(DOB, '%m/%d/%Y')
except:
flist.append(line.replace('n', ''))
raise ValueError("DATE NOT IN RIGHT FORMAT")
I have fed the program multiple lines, and the one in particular I'm feeding:
NAME|FAKE|M|09/20/1987
ValueError: time data '09/20/1987' does not match format '%d/%m/%Y'
I've printed out the fields and I've tried converting "DOB" to a string. I've tried appending the .date()
to the end as well. I'm really not sure why it would be failing.
python python-datetime
add a comment |
My program takes in data in the form of:
LASTNAME|FIRSTNAME|GENDER|DOB
The first thing I do is use regular expressions to detect the delimiter and split the fields. I allow a space, comma, or pipe as a delimiter. I know which field is DOB and have printed it out to ensure I'm not dealing with the wrong field.
My try code is as follows:
try:
#check if the fields are good
fields = re.split(r'[ ,|]+', line)
except:
#if not good: put it on the failure list
flist.append(line.replace('n', ''))
LastName = fields[0]
FirstName = fields[1]
Gender = fields[2]
DOB = fields[3]
#one last try... make sure the DOB is good
try:
datetime.datetime.strptime(DOB, '%m/%d/%Y')
except:
flist.append(line.replace('n', ''))
raise ValueError("DATE NOT IN RIGHT FORMAT")
I have fed the program multiple lines, and the one in particular I'm feeding:
NAME|FAKE|M|09/20/1987
ValueError: time data '09/20/1987' does not match format '%d/%m/%Y'
I've printed out the fields and I've tried converting "DOB" to a string. I've tried appending the .date()
to the end as well. I'm really not sure why it would be failing.
python python-datetime
3
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17
add a comment |
My program takes in data in the form of:
LASTNAME|FIRSTNAME|GENDER|DOB
The first thing I do is use regular expressions to detect the delimiter and split the fields. I allow a space, comma, or pipe as a delimiter. I know which field is DOB and have printed it out to ensure I'm not dealing with the wrong field.
My try code is as follows:
try:
#check if the fields are good
fields = re.split(r'[ ,|]+', line)
except:
#if not good: put it on the failure list
flist.append(line.replace('n', ''))
LastName = fields[0]
FirstName = fields[1]
Gender = fields[2]
DOB = fields[3]
#one last try... make sure the DOB is good
try:
datetime.datetime.strptime(DOB, '%m/%d/%Y')
except:
flist.append(line.replace('n', ''))
raise ValueError("DATE NOT IN RIGHT FORMAT")
I have fed the program multiple lines, and the one in particular I'm feeding:
NAME|FAKE|M|09/20/1987
ValueError: time data '09/20/1987' does not match format '%d/%m/%Y'
I've printed out the fields and I've tried converting "DOB" to a string. I've tried appending the .date()
to the end as well. I'm really not sure why it would be failing.
python python-datetime
My program takes in data in the form of:
LASTNAME|FIRSTNAME|GENDER|DOB
The first thing I do is use regular expressions to detect the delimiter and split the fields. I allow a space, comma, or pipe as a delimiter. I know which field is DOB and have printed it out to ensure I'm not dealing with the wrong field.
My try code is as follows:
try:
#check if the fields are good
fields = re.split(r'[ ,|]+', line)
except:
#if not good: put it on the failure list
flist.append(line.replace('n', ''))
LastName = fields[0]
FirstName = fields[1]
Gender = fields[2]
DOB = fields[3]
#one last try... make sure the DOB is good
try:
datetime.datetime.strptime(DOB, '%m/%d/%Y')
except:
flist.append(line.replace('n', ''))
raise ValueError("DATE NOT IN RIGHT FORMAT")
I have fed the program multiple lines, and the one in particular I'm feeding:
NAME|FAKE|M|09/20/1987
ValueError: time data '09/20/1987' does not match format '%d/%m/%Y'
I've printed out the fields and I've tried converting "DOB" to a string. I've tried appending the .date()
to the end as well. I'm really not sure why it would be failing.
python python-datetime
python python-datetime
edited Nov 20 '18 at 17:58


jonrsharpe
77.3k11104209
77.3k11104209
asked Nov 20 '18 at 17:56
Mike ThomaMike Thoma
192
192
3
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17
add a comment |
3
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17
3
3
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17
add a comment |
1 Answer
1
active
oldest
votes
@jonrsharpe is right. You're trying to parse a MM/DD/YYYY string as DD/MM/YYYY. If all of your dates are in the same format, you should be using '%d/%m/%Y'
as your format string.
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398830%2fpython-checking-date-format-failing-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
@jonrsharpe is right. You're trying to parse a MM/DD/YYYY string as DD/MM/YYYY. If all of your dates are in the same format, you should be using '%d/%m/%Y'
as your format string.
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
add a comment |
@jonrsharpe is right. You're trying to parse a MM/DD/YYYY string as DD/MM/YYYY. If all of your dates are in the same format, you should be using '%d/%m/%Y'
as your format string.
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
add a comment |
@jonrsharpe is right. You're trying to parse a MM/DD/YYYY string as DD/MM/YYYY. If all of your dates are in the same format, you should be using '%d/%m/%Y'
as your format string.
@jonrsharpe is right. You're trying to parse a MM/DD/YYYY string as DD/MM/YYYY. If all of your dates are in the same format, you should be using '%d/%m/%Y'
as your format string.
answered Nov 20 '18 at 18:44
WieschieWieschie
169110
169110
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
add a comment |
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
1
1
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398830%2fpython-checking-date-format-failing-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What's the 20th month? It looks like you're getting US-style dates, MM/DD/YYYY.
– jonrsharpe
Nov 20 '18 at 17:57
Well that's embarrassing. I was banging my head against the wall figuring out why some records were processing fine and others weren't. I read through the documentation tons of times and failed to notice the mix up. Thank you so much!
– Mike Thoma
Nov 20 '18 at 21:17