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;
}







1















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









share|improve this question





























    1















    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









    share|improve this question

























      1












      1








      1








      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









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 8:37









      Chris PattonChris Patton

      132




      132
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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]






          share|improve this answer
























          • 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



















          0














          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





          share|improve this answer


























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            0














            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]






            share|improve this answer
























            • 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
















            0














            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]






            share|improve this answer
























            • 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














            0












            0








            0







            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]






            share|improve this answer













            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]







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            0














            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





            share|improve this answer






























              0














              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





              share|improve this answer




























                0












                0








                0







                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





                share|improve this answer















                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 25 '18 at 11:40









                Tomothy32

                9,1672928




                9,1672928










                answered Nov 25 '18 at 8:48









                Anurag ChoudharyAnurag Choudhary

                10119




                10119






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings