How to print set list to new lines in text file python 3












-2















Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.



For example;
input.txt contains different lines with text



Then I use this to convert to list:



with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]


Then outputting the list into a text file not like ['string', 'string'] but on new lines.



Any help with this???










share|improve this question

























  • What have you tried? This sounds like a homework assignment.

    – John Coleman
    Nov 24 '18 at 10:53











  • What have I tried? What do you mean?

    – DMM Official
    Nov 24 '18 at 10:54






  • 1





    what have you done to try solving the problem yourself? @DMMOfficial

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:57











  • I have searched up online and stack, but I'm just getting started with coding tbh so yeah

    – DMM Official
    Nov 24 '18 at 11:07
















-2















Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.



For example;
input.txt contains different lines with text



Then I use this to convert to list:



with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]


Then outputting the list into a text file not like ['string', 'string'] but on new lines.



Any help with this???










share|improve this question

























  • What have you tried? This sounds like a homework assignment.

    – John Coleman
    Nov 24 '18 at 10:53











  • What have I tried? What do you mean?

    – DMM Official
    Nov 24 '18 at 10:54






  • 1





    what have you done to try solving the problem yourself? @DMMOfficial

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:57











  • I have searched up online and stack, but I'm just getting started with coding tbh so yeah

    – DMM Official
    Nov 24 '18 at 11:07














-2












-2








-2








Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.



For example;
input.txt contains different lines with text



Then I use this to convert to list:



with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]


Then outputting the list into a text file not like ['string', 'string'] but on new lines.



Any help with this???










share|improve this question
















Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.



For example;
input.txt contains different lines with text



Then I use this to convert to list:



with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]


Then outputting the list into a text file not like ['string', 'string'] but on new lines.



Any help with this???







python python-3.x list file file-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 10:54







DMM Official

















asked Nov 24 '18 at 10:50









DMM OfficialDMM Official

73




73













  • What have you tried? This sounds like a homework assignment.

    – John Coleman
    Nov 24 '18 at 10:53











  • What have I tried? What do you mean?

    – DMM Official
    Nov 24 '18 at 10:54






  • 1





    what have you done to try solving the problem yourself? @DMMOfficial

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:57











  • I have searched up online and stack, but I'm just getting started with coding tbh so yeah

    – DMM Official
    Nov 24 '18 at 11:07



















  • What have you tried? This sounds like a homework assignment.

    – John Coleman
    Nov 24 '18 at 10:53











  • What have I tried? What do you mean?

    – DMM Official
    Nov 24 '18 at 10:54






  • 1





    what have you done to try solving the problem yourself? @DMMOfficial

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:57











  • I have searched up online and stack, but I'm just getting started with coding tbh so yeah

    – DMM Official
    Nov 24 '18 at 11:07

















What have you tried? This sounds like a homework assignment.

– John Coleman
Nov 24 '18 at 10:53





What have you tried? This sounds like a homework assignment.

– John Coleman
Nov 24 '18 at 10:53













What have I tried? What do you mean?

– DMM Official
Nov 24 '18 at 10:54





What have I tried? What do you mean?

– DMM Official
Nov 24 '18 at 10:54




1




1





what have you done to try solving the problem yourself? @DMMOfficial

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57





what have you done to try solving the problem yourself? @DMMOfficial

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57













I have searched up online and stack, but I'm just getting started with coding tbh so yeah

– DMM Official
Nov 24 '18 at 11:07





I have searched up online and stack, but I'm just getting started with coding tbh so yeah

– DMM Official
Nov 24 '18 at 11:07












3 Answers
3






active

oldest

votes


















0














After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:



with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)





share|improve this answer































    0














    $ cat input.txt
    HELLO
    WORLD


    In python you can use 'n' to append a new line to content.



       with open("input.txt") as f:
    content = f.readlines()
    content = [x.strip() for x in content]
    content = 'n'.join(content)





    share|improve this answer































      0














      You can create a string like this:



      elements_on_new_line = 'n'.join(my_list)


      And then you write on the file the variabile 'elements_on_new_line'



      Doing this



      with open('my file.txt', 'w') as my_file:
      my_file.write(elements_on_new_line)





      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%2f53457377%2fhow-to-print-set-list-to-new-lines-in-text-file-python-3%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:



        with open('my_file.txt', 'w') as f:
        for item in my_list:
        f.write("%sn" % item)





        share|improve this answer




























          0














          After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:



          with open('my_file.txt', 'w') as f:
          for item in my_list:
          f.write("%sn" % item)





          share|improve this answer


























            0












            0








            0







            After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:



            with open('my_file.txt', 'w') as f:
            for item in my_list:
            f.write("%sn" % item)





            share|improve this answer













            After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:



            with open('my_file.txt', 'w') as f:
            for item in my_list:
            f.write("%sn" % item)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 11:02









            Sonal BorkarSonal Borkar

            520311




            520311

























                0














                $ cat input.txt
                HELLO
                WORLD


                In python you can use 'n' to append a new line to content.



                   with open("input.txt") as f:
                content = f.readlines()
                content = [x.strip() for x in content]
                content = 'n'.join(content)





                share|improve this answer




























                  0














                  $ cat input.txt
                  HELLO
                  WORLD


                  In python you can use 'n' to append a new line to content.



                     with open("input.txt") as f:
                  content = f.readlines()
                  content = [x.strip() for x in content]
                  content = 'n'.join(content)





                  share|improve this answer


























                    0












                    0








                    0







                    $ cat input.txt
                    HELLO
                    WORLD


                    In python you can use 'n' to append a new line to content.



                       with open("input.txt") as f:
                    content = f.readlines()
                    content = [x.strip() for x in content]
                    content = 'n'.join(content)





                    share|improve this answer













                    $ cat input.txt
                    HELLO
                    WORLD


                    In python you can use 'n' to append a new line to content.



                       with open("input.txt") as f:
                    content = f.readlines()
                    content = [x.strip() for x in content]
                    content = 'n'.join(content)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 11:02









                    Arbazz HussainArbazz Hussain

                    144114




                    144114























                        0














                        You can create a string like this:



                        elements_on_new_line = 'n'.join(my_list)


                        And then you write on the file the variabile 'elements_on_new_line'



                        Doing this



                        with open('my file.txt', 'w') as my_file:
                        my_file.write(elements_on_new_line)





                        share|improve this answer






























                          0














                          You can create a string like this:



                          elements_on_new_line = 'n'.join(my_list)


                          And then you write on the file the variabile 'elements_on_new_line'



                          Doing this



                          with open('my file.txt', 'w') as my_file:
                          my_file.write(elements_on_new_line)





                          share|improve this answer




























                            0












                            0








                            0







                            You can create a string like this:



                            elements_on_new_line = 'n'.join(my_list)


                            And then you write on the file the variabile 'elements_on_new_line'



                            Doing this



                            with open('my file.txt', 'w') as my_file:
                            my_file.write(elements_on_new_line)





                            share|improve this answer















                            You can create a string like this:



                            elements_on_new_line = 'n'.join(my_list)


                            And then you write on the file the variabile 'elements_on_new_line'



                            Doing this



                            with open('my file.txt', 'w') as my_file:
                            my_file.write(elements_on_new_line)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 24 '18 at 11:17

























                            answered Nov 24 '18 at 11:06









                            91DarioDev91DarioDev

                            714316




                            714316






























                                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%2f53457377%2fhow-to-print-set-list-to-new-lines-in-text-file-python-3%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

                                Create new schema in PostgreSQL using DBeaver

                                Deepest pit of an array with Javascript: test on Codility

                                Costa Masnaga