how to reformat a text paragrath using python [closed]










0















Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.










share|improve this question













closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

    – cole
    Nov 13 '18 at 20:42












  • Possible duplicate of Split a string by a delimiter in python

    – mrk
    Nov 13 '18 at 21:13















0















Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.










share|improve this question













closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















  • Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

    – cole
    Nov 13 '18 at 20:42












  • Possible duplicate of Split a string by a delimiter in python

    – mrk
    Nov 13 '18 at 21:13













0












0








0








Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.










share|improve this question














Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.







python string text






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:39









bmanbman

1




1




closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

    – cole
    Nov 13 '18 at 20:42












  • Possible duplicate of Split a string by a delimiter in python

    – mrk
    Nov 13 '18 at 21:13

















  • Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

    – cole
    Nov 13 '18 at 20:42












  • Possible duplicate of Split a string by a delimiter in python

    – mrk
    Nov 13 '18 at 21:13
















Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

– cole
Nov 13 '18 at 20:42






Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like sed ‘s/,/,n/’

– cole
Nov 13 '18 at 20:42














Possible duplicate of Split a string by a delimiter in python

– mrk
Nov 13 '18 at 21:13





Possible duplicate of Split a string by a delimiter in python

– mrk
Nov 13 '18 at 21:13












2 Answers
2






active

oldest

votes


















1














You can do using str.replace() in python. Check out the below code, replacing every , with ,n.



string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)


File before execution:



Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,


After Execution:



Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,





share|improve this answer






























    0














    you can use the ''.replace() method like so:



    'roses can be blue, red, white'.replace(',' , ',n') gives
    'roses can be blue,n red,n white' efectively inserting 'n' after every ,






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can do using str.replace() in python. Check out the below code, replacing every , with ,n.



      string = ""
      with open('test.txt','r') as myfile:
      for line in myfile:
      string += line.replace(",",",n")
      myfile.close()
      myfile = open('test.txt','w')
      myfile.write(string)


      File before execution:



      Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,


      After Execution:



      Hello World and again HelloWorld,
      sdjakljsljsfs,
      asdgrwcfdssaasf,
      sdfoieunvsfaf,
      asdasdafjslkj,





      share|improve this answer



























        1














        You can do using str.replace() in python. Check out the below code, replacing every , with ,n.



        string = ""
        with open('test.txt','r') as myfile:
        for line in myfile:
        string += line.replace(",",",n")
        myfile.close()
        myfile = open('test.txt','w')
        myfile.write(string)


        File before execution:



        Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,


        After Execution:



        Hello World and again HelloWorld,
        sdjakljsljsfs,
        asdgrwcfdssaasf,
        sdfoieunvsfaf,
        asdasdafjslkj,





        share|improve this answer

























          1












          1








          1







          You can do using str.replace() in python. Check out the below code, replacing every , with ,n.



          string = ""
          with open('test.txt','r') as myfile:
          for line in myfile:
          string += line.replace(",",",n")
          myfile.close()
          myfile = open('test.txt','w')
          myfile.write(string)


          File before execution:



          Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,


          After Execution:



          Hello World and again HelloWorld,
          sdjakljsljsfs,
          asdgrwcfdssaasf,
          sdfoieunvsfaf,
          asdasdafjslkj,





          share|improve this answer













          You can do using str.replace() in python. Check out the below code, replacing every , with ,n.



          string = ""
          with open('test.txt','r') as myfile:
          for line in myfile:
          string += line.replace(",",",n")
          myfile.close()
          myfile = open('test.txt','w')
          myfile.write(string)


          File before execution:



          Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,


          After Execution:



          Hello World and again HelloWorld,
          sdjakljsljsfs,
          asdgrwcfdssaasf,
          sdfoieunvsfaf,
          asdasdafjslkj,






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 20:51









          Sanchit KumarSanchit Kumar

          36319




          36319























              0














              you can use the ''.replace() method like so:



              'roses can be blue, red, white'.replace(',' , ',n') gives
              'roses can be blue,n red,n white' efectively inserting 'n' after every ,






              share|improve this answer



























                0














                you can use the ''.replace() method like so:



                'roses can be blue, red, white'.replace(',' , ',n') gives
                'roses can be blue,n red,n white' efectively inserting 'n' after every ,






                share|improve this answer

























                  0












                  0








                  0







                  you can use the ''.replace() method like so:



                  'roses can be blue, red, white'.replace(',' , ',n') gives
                  'roses can be blue,n red,n white' efectively inserting 'n' after every ,






                  share|improve this answer













                  you can use the ''.replace() method like so:



                  'roses can be blue, red, white'.replace(',' , ',n') gives
                  'roses can be blue,n red,n white' efectively inserting 'n' after every ,







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 20:42









                  vencaslacvencaslac

                  1,070317




                  1,070317













                      Popular posts from this blog

                      Darth Vader #20

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Ondo