What is the best way in xonsh to loop over the lines of a file?












2














What is the best way in the xonsh shell to loop over the lines of a text file?



(A) At the moment I'm using



for l in !(cat file.txt): 
line = l.strip()
# Do something with line...


(B) Of course, there is also



with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...


I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip() into the loop?



Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.










share|improve this question





























    2














    What is the best way in the xonsh shell to loop over the lines of a text file?



    (A) At the moment I'm using



    for l in !(cat file.txt): 
    line = l.strip()
    # Do something with line...


    (B) Of course, there is also



    with open(p'file.txt') as f:
    for l in f:
    line = l.strip()
    # Do something with line...


    I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip() into the loop?



    Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.










    share|improve this question



























      2












      2








      2







      What is the best way in the xonsh shell to loop over the lines of a text file?



      (A) At the moment I'm using



      for l in !(cat file.txt): 
      line = l.strip()
      # Do something with line...


      (B) Of course, there is also



      with open(p'file.txt') as f:
      for l in f:
      line = l.strip()
      # Do something with line...


      I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip() into the loop?



      Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.










      share|improve this question















      What is the best way in the xonsh shell to loop over the lines of a text file?



      (A) At the moment I'm using



      for l in !(cat file.txt): 
      line = l.strip()
      # Do something with line...


      (B) Of course, there is also



      with open(p'file.txt') as f:
      for l in f:
      line = l.strip()
      # Do something with line...


      I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip() into the loop?



      Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.







      python bash xonsh






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 2:33

























      asked Nov 12 at 0:31









      halloleo

      2,37042258




      2,37042258
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You can fold str.strip() into the loop with map():



          (A):



          for l in map(str.strip, !(cat file.txt)):
          # Do something with line...


          (B):



          with open('file.txt') as f:
          for l in map(str.strip, f):
          # Do something with l..





          share|improve this answer























          • Yes, both versions work. I have just tested them in xonsh 0.8.3.
            – halloleo
            Nov 12 at 2:39





















          1














          Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:



          for l in map(str.strip, open('file.txt')):
          # do stuff with l


          Or using the p'' string to make a path in xonsh (this does properly close the file):



          for l in p'file.txt'.read_text().splitlines():
          # do stuff with l


          splitlines() already removes the new line characters, but not other whitespace.






          share|improve this answer























          • I wouldn't say this is in any way more concise than the code in the question...
            – halloleo
            Nov 12 at 2:29










          • True...I misinterpreted your need for conciseness as a need for readability.
            – soundstripe
            Nov 12 at 2:59










          • Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
            – soundstripe
            Nov 12 at 14:34










          • Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
            – halloleo
            Nov 13 at 11:18










          • Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
            – halloleo
            Nov 14 at 0:58











          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%2f53254629%2fwhat-is-the-best-way-in-xonsh-to-loop-over-the-lines-of-a-file%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









          2














          You can fold str.strip() into the loop with map():



          (A):



          for l in map(str.strip, !(cat file.txt)):
          # Do something with line...


          (B):



          with open('file.txt') as f:
          for l in map(str.strip, f):
          # Do something with l..





          share|improve this answer























          • Yes, both versions work. I have just tested them in xonsh 0.8.3.
            – halloleo
            Nov 12 at 2:39


















          2














          You can fold str.strip() into the loop with map():



          (A):



          for l in map(str.strip, !(cat file.txt)):
          # Do something with line...


          (B):



          with open('file.txt') as f:
          for l in map(str.strip, f):
          # Do something with l..





          share|improve this answer























          • Yes, both versions work. I have just tested them in xonsh 0.8.3.
            – halloleo
            Nov 12 at 2:39
















          2












          2








          2






          You can fold str.strip() into the loop with map():



          (A):



          for l in map(str.strip, !(cat file.txt)):
          # Do something with line...


          (B):



          with open('file.txt') as f:
          for l in map(str.strip, f):
          # Do something with l..





          share|improve this answer














          You can fold str.strip() into the loop with map():



          (A):



          for l in map(str.strip, !(cat file.txt)):
          # Do something with line...


          (B):



          with open('file.txt') as f:
          for l in map(str.strip, f):
          # Do something with l..






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 1:30









          halloleo

          2,37042258




          2,37042258










          answered Nov 12 at 0:55









          RoadRunner

          9,92531240




          9,92531240












          • Yes, both versions work. I have just tested them in xonsh 0.8.3.
            – halloleo
            Nov 12 at 2:39




















          • Yes, both versions work. I have just tested them in xonsh 0.8.3.
            – halloleo
            Nov 12 at 2:39


















          Yes, both versions work. I have just tested them in xonsh 0.8.3.
          – halloleo
          Nov 12 at 2:39






          Yes, both versions work. I have just tested them in xonsh 0.8.3.
          – halloleo
          Nov 12 at 2:39















          1














          Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:



          for l in map(str.strip, open('file.txt')):
          # do stuff with l


          Or using the p'' string to make a path in xonsh (this does properly close the file):



          for l in p'file.txt'.read_text().splitlines():
          # do stuff with l


          splitlines() already removes the new line characters, but not other whitespace.






          share|improve this answer























          • I wouldn't say this is in any way more concise than the code in the question...
            – halloleo
            Nov 12 at 2:29










          • True...I misinterpreted your need for conciseness as a need for readability.
            – soundstripe
            Nov 12 at 2:59










          • Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
            – soundstripe
            Nov 12 at 14:34










          • Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
            – halloleo
            Nov 13 at 11:18










          • Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
            – halloleo
            Nov 14 at 0:58
















          1














          Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:



          for l in map(str.strip, open('file.txt')):
          # do stuff with l


          Or using the p'' string to make a path in xonsh (this does properly close the file):



          for l in p'file.txt'.read_text().splitlines():
          # do stuff with l


          splitlines() already removes the new line characters, but not other whitespace.






          share|improve this answer























          • I wouldn't say this is in any way more concise than the code in the question...
            – halloleo
            Nov 12 at 2:29










          • True...I misinterpreted your need for conciseness as a need for readability.
            – soundstripe
            Nov 12 at 2:59










          • Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
            – soundstripe
            Nov 12 at 14:34










          • Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
            – halloleo
            Nov 13 at 11:18










          • Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
            – halloleo
            Nov 14 at 0:58














          1












          1








          1






          Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:



          for l in map(str.strip, open('file.txt')):
          # do stuff with l


          Or using the p'' string to make a path in xonsh (this does properly close the file):



          for l in p'file.txt'.read_text().splitlines():
          # do stuff with l


          splitlines() already removes the new line characters, but not other whitespace.






          share|improve this answer














          Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:



          for l in map(str.strip, open('file.txt')):
          # do stuff with l


          Or using the p'' string to make a path in xonsh (this does properly close the file):



          for l in p'file.txt'.read_text().splitlines():
          # do stuff with l


          splitlines() already removes the new line characters, but not other whitespace.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 at 0:56









          halloleo

          2,37042258




          2,37042258










          answered Nov 12 at 1:27









          soundstripe

          47138




          47138












          • I wouldn't say this is in any way more concise than the code in the question...
            – halloleo
            Nov 12 at 2:29










          • True...I misinterpreted your need for conciseness as a need for readability.
            – soundstripe
            Nov 12 at 2:59










          • Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
            – soundstripe
            Nov 12 at 14:34










          • Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
            – halloleo
            Nov 13 at 11:18










          • Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
            – halloleo
            Nov 14 at 0:58


















          • I wouldn't say this is in any way more concise than the code in the question...
            – halloleo
            Nov 12 at 2:29










          • True...I misinterpreted your need for conciseness as a need for readability.
            – soundstripe
            Nov 12 at 2:59










          • Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
            – soundstripe
            Nov 12 at 14:34










          • Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
            – halloleo
            Nov 13 at 11:18










          • Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
            – halloleo
            Nov 14 at 0:58
















          I wouldn't say this is in any way more concise than the code in the question...
          – halloleo
          Nov 12 at 2:29




          I wouldn't say this is in any way more concise than the code in the question...
          – halloleo
          Nov 12 at 2:29












          True...I misinterpreted your need for conciseness as a need for readability.
          – soundstripe
          Nov 12 at 2:59




          True...I misinterpreted your need for conciseness as a need for readability.
          – soundstripe
          Nov 12 at 2:59












          Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
          – soundstripe
          Nov 12 at 14:34




          Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
          – soundstripe
          Nov 12 at 14:34












          Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
          – halloleo
          Nov 13 at 11:18




          Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
          – halloleo
          Nov 13 at 11:18












          Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
          – halloleo
          Nov 14 at 0:58




          Just realised that the method on Path objects is called read_text, not get_text. Edited the answer accordingly.
          – halloleo
          Nov 14 at 0:58


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53254629%2fwhat-is-the-best-way-in-xonsh-to-loop-over-the-lines-of-a-file%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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma