How to limit the length of each line in a text file using python [duplicate]












-2















This question already has an answer here:




  • set a limit to the number of words or characters in a string

    3 answers




I have a text file of following type-



 
eng Firstly, in the course of the last few decades the national
eng Secondly, the national courts will be empowered to implement
eng However, I am convinced of the fact that the White Paper has put us on
the right path.



I want to restrict the length of each line upto (say) 9 words.
I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?



Sample Output-



eng Firstly, in the course of the last few
eng Secondly, the national courts will be empowered to
eng However, I am convinced of the fact that









share|improve this question













marked as duplicate by MSeifert, Martijn Pieters python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    -2















    This question already has an answer here:




    • set a limit to the number of words or characters in a string

      3 answers




    I have a text file of following type-



     
    eng Firstly, in the course of the last few decades the national
    eng Secondly, the national courts will be empowered to implement
    eng However, I am convinced of the fact that the White Paper has put us on
    the right path.



    I want to restrict the length of each line upto (say) 9 words.
    I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?



    Sample Output-



    eng Firstly, in the course of the last few
    eng Secondly, the national courts will be empowered to
    eng However, I am convinced of the fact that









    share|improve this question













    marked as duplicate by MSeifert, Martijn Pieters python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 12 '18 at 19:20


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      -2












      -2








      -2








      This question already has an answer here:




      • set a limit to the number of words or characters in a string

        3 answers




      I have a text file of following type-



       
      eng Firstly, in the course of the last few decades the national
      eng Secondly, the national courts will be empowered to implement
      eng However, I am convinced of the fact that the White Paper has put us on
      the right path.



      I want to restrict the length of each line upto (say) 9 words.
      I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?



      Sample Output-



      eng Firstly, in the course of the last few
      eng Secondly, the national courts will be empowered to
      eng However, I am convinced of the fact that









      share|improve this question














      This question already has an answer here:




      • set a limit to the number of words or characters in a string

        3 answers




      I have a text file of following type-



       
      eng Firstly, in the course of the last few decades the national
      eng Secondly, the national courts will be empowered to implement
      eng However, I am convinced of the fact that the White Paper has put us on
      the right path.



      I want to restrict the length of each line upto (say) 9 words.
      I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?



      Sample Output-



      eng Firstly, in the course of the last few
      eng Secondly, the national courts will be empowered to
      eng However, I am convinced of the fact that




      This question already has an answer here:




      • set a limit to the number of words or characters in a string

        3 answers








      python python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 19:11









      Bing

      86112




      86112




      marked as duplicate by MSeifert, Martijn Pieters python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 '18 at 19:20


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by MSeifert, Martijn Pieters python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 '18 at 19:20


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          5














          To get the first n words of a string as a string:



          def first_n_words(s, n):
          return ' '.join(s.split()[:n])





          share|improve this answer





























            1














            You could make a list of each word like so:



            with open(file, 'r') as f:
            lines =
            for line in f:
            lines.append(line.rstrip('n').split())


            Now limit each line to 9 by using slicing which truncated automatically:



            with open(file, 'w') as f:
            for line in lines:
            f.write(' '.join(line[:9]))
            f.write('n')





            share|improve this answer




























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              To get the first n words of a string as a string:



              def first_n_words(s, n):
              return ' '.join(s.split()[:n])





              share|improve this answer


























                5














                To get the first n words of a string as a string:



                def first_n_words(s, n):
                return ' '.join(s.split()[:n])





                share|improve this answer
























                  5












                  5








                  5






                  To get the first n words of a string as a string:



                  def first_n_words(s, n):
                  return ' '.join(s.split()[:n])





                  share|improve this answer












                  To get the first n words of a string as a string:



                  def first_n_words(s, n):
                  return ' '.join(s.split()[:n])






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 19:14









                  MegaBluejay

                  35118




                  35118

























                      1














                      You could make a list of each word like so:



                      with open(file, 'r') as f:
                      lines =
                      for line in f:
                      lines.append(line.rstrip('n').split())


                      Now limit each line to 9 by using slicing which truncated automatically:



                      with open(file, 'w') as f:
                      for line in lines:
                      f.write(' '.join(line[:9]))
                      f.write('n')





                      share|improve this answer


























                        1














                        You could make a list of each word like so:



                        with open(file, 'r') as f:
                        lines =
                        for line in f:
                        lines.append(line.rstrip('n').split())


                        Now limit each line to 9 by using slicing which truncated automatically:



                        with open(file, 'w') as f:
                        for line in lines:
                        f.write(' '.join(line[:9]))
                        f.write('n')





                        share|improve this answer
























                          1












                          1








                          1






                          You could make a list of each word like so:



                          with open(file, 'r') as f:
                          lines =
                          for line in f:
                          lines.append(line.rstrip('n').split())


                          Now limit each line to 9 by using slicing which truncated automatically:



                          with open(file, 'w') as f:
                          for line in lines:
                          f.write(' '.join(line[:9]))
                          f.write('n')





                          share|improve this answer












                          You could make a list of each word like so:



                          with open(file, 'r') as f:
                          lines =
                          for line in f:
                          lines.append(line.rstrip('n').split())


                          Now limit each line to 9 by using slicing which truncated automatically:



                          with open(file, 'w') as f:
                          for line in lines:
                          f.write(' '.join(line[:9]))
                          f.write('n')






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 '18 at 19:20









                          N Chauhan

                          1,6941214




                          1,6941214















                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()