Python Strings not matching
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am pretty new to Python, trying to calculate some time, get two strings from different sources to be equal, but the are FALSE, when tested. First string is pulled sliced from a single line file, the other is set as a variable in the code. Both show as str, same apparent value, but when checked to be equal, they are FALSE.
from datetime import datetime
file=open("test.db","r")
for i in file:
print(i)
print(repr(i))
print(str(i.rstrip('n')))
ci_time = (str((i.rstrip('n'))[14:20]))
print(ci_time)
ci_time1 = '08:00'
print(type(ci_time))
print(ci_time)
print(type(ci_time1))
print(ci_time1)
print(ci_time == ci_time1)
file.close()
Here is my output...
$ python3 test.py
CI 2018-11-23 08:00 CO 2018-11-23 17:00
'CI 2018-11-23 08:00 CO 2018-11-23 17:00n'
CI 2018-11-23 08:00 CO 2018-11-23 17:00
08:00
<class 'str'>
08:00
<class 'str'>
08:00
False
python
add a comment |
I am pretty new to Python, trying to calculate some time, get two strings from different sources to be equal, but the are FALSE, when tested. First string is pulled sliced from a single line file, the other is set as a variable in the code. Both show as str, same apparent value, but when checked to be equal, they are FALSE.
from datetime import datetime
file=open("test.db","r")
for i in file:
print(i)
print(repr(i))
print(str(i.rstrip('n')))
ci_time = (str((i.rstrip('n'))[14:20]))
print(ci_time)
ci_time1 = '08:00'
print(type(ci_time))
print(ci_time)
print(type(ci_time1))
print(ci_time1)
print(ci_time == ci_time1)
file.close()
Here is my output...
$ python3 test.py
CI 2018-11-23 08:00 CO 2018-11-23 17:00
'CI 2018-11-23 08:00 CO 2018-11-23 17:00n'
CI 2018-11-23 08:00 CO 2018-11-23 17:00
08:00
<class 'str'>
08:00
<class 'str'>
08:00
False
python
add a comment |
I am pretty new to Python, trying to calculate some time, get two strings from different sources to be equal, but the are FALSE, when tested. First string is pulled sliced from a single line file, the other is set as a variable in the code. Both show as str, same apparent value, but when checked to be equal, they are FALSE.
from datetime import datetime
file=open("test.db","r")
for i in file:
print(i)
print(repr(i))
print(str(i.rstrip('n')))
ci_time = (str((i.rstrip('n'))[14:20]))
print(ci_time)
ci_time1 = '08:00'
print(type(ci_time))
print(ci_time)
print(type(ci_time1))
print(ci_time1)
print(ci_time == ci_time1)
file.close()
Here is my output...
$ python3 test.py
CI 2018-11-23 08:00 CO 2018-11-23 17:00
'CI 2018-11-23 08:00 CO 2018-11-23 17:00n'
CI 2018-11-23 08:00 CO 2018-11-23 17:00
08:00
<class 'str'>
08:00
<class 'str'>
08:00
False
python
I am pretty new to Python, trying to calculate some time, get two strings from different sources to be equal, but the are FALSE, when tested. First string is pulled sliced from a single line file, the other is set as a variable in the code. Both show as str, same apparent value, but when checked to be equal, they are FALSE.
from datetime import datetime
file=open("test.db","r")
for i in file:
print(i)
print(repr(i))
print(str(i.rstrip('n')))
ci_time = (str((i.rstrip('n'))[14:20]))
print(ci_time)
ci_time1 = '08:00'
print(type(ci_time))
print(ci_time)
print(type(ci_time1))
print(ci_time1)
print(ci_time == ci_time1)
file.close()
Here is my output...
$ python3 test.py
CI 2018-11-23 08:00 CO 2018-11-23 17:00
'CI 2018-11-23 08:00 CO 2018-11-23 17:00n'
CI 2018-11-23 08:00 CO 2018-11-23 17:00
08:00
<class 'str'>
08:00
<class 'str'>
08:00
False
python
python
asked Nov 25 '18 at 8:37
Chris PattonChris Patton
132
132
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
So if you look carefully you will see that your ci_time variable actually has a trailing space:
ci_time
>>'08:00 '
You can remove this using:
ci_time.strip()
>>'08:00'
Or alternatively you can update the index in your string slice to be [14:19]
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
add a comment |
Try adding strip() to ci_time as given below, you need to strip the space in ci_time.
print(ci_time.strip() == ci_time1)
ci = 'CI 2018-11-23 08:00 CO 2018-11-23 17:00'
print len(str(ci[14:20])) --6
print len(str(ci[14:20]).strip()) --5
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%2f53465888%2fpython-strings-not-matching%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So if you look carefully you will see that your ci_time variable actually has a trailing space:
ci_time
>>'08:00 '
You can remove this using:
ci_time.strip()
>>'08:00'
Or alternatively you can update the index in your string slice to be [14:19]
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
add a comment |
So if you look carefully you will see that your ci_time variable actually has a trailing space:
ci_time
>>'08:00 '
You can remove this using:
ci_time.strip()
>>'08:00'
Or alternatively you can update the index in your string slice to be [14:19]
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
add a comment |
So if you look carefully you will see that your ci_time variable actually has a trailing space:
ci_time
>>'08:00 '
You can remove this using:
ci_time.strip()
>>'08:00'
Or alternatively you can update the index in your string slice to be [14:19]
So if you look carefully you will see that your ci_time variable actually has a trailing space:
ci_time
>>'08:00 '
You can remove this using:
ci_time.strip()
>>'08:00'
Or alternatively you can update the index in your string slice to be [14:19]
answered Nov 25 '18 at 8:45
Sven HarrisSven Harris
2,1961516
2,1961516
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
add a comment |
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
Thanks, I hadn't tried it from the interpreter (lesson learned). I updated the slicing, thanks for the answer.
– Chris Patton
Nov 25 '18 at 15:03
add a comment |
Try adding strip() to ci_time as given below, you need to strip the space in ci_time.
print(ci_time.strip() == ci_time1)
ci = 'CI 2018-11-23 08:00 CO 2018-11-23 17:00'
print len(str(ci[14:20])) --6
print len(str(ci[14:20]).strip()) --5
add a comment |
Try adding strip() to ci_time as given below, you need to strip the space in ci_time.
print(ci_time.strip() == ci_time1)
ci = 'CI 2018-11-23 08:00 CO 2018-11-23 17:00'
print len(str(ci[14:20])) --6
print len(str(ci[14:20]).strip()) --5
add a comment |
Try adding strip() to ci_time as given below, you need to strip the space in ci_time.
print(ci_time.strip() == ci_time1)
ci = 'CI 2018-11-23 08:00 CO 2018-11-23 17:00'
print len(str(ci[14:20])) --6
print len(str(ci[14:20]).strip()) --5
Try adding strip() to ci_time as given below, you need to strip the space in ci_time.
print(ci_time.strip() == ci_time1)
ci = 'CI 2018-11-23 08:00 CO 2018-11-23 17:00'
print len(str(ci[14:20])) --6
print len(str(ci[14:20]).strip()) --5
edited Nov 25 '18 at 11:40
Tomothy32
9,1672928
9,1672928
answered Nov 25 '18 at 8:48
Anurag ChoudharyAnurag Choudhary
10119
10119
add a comment |
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%2f53465888%2fpython-strings-not-matching%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