How to turn a list into a column in a csv?












2















Currently I have a populated list



list =[a, b, c, d]


I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:



with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)


The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.



I have tried making a for loop to write each element + n but the writerow method has an issue with that (puts commas after each letter), and there is no writecolumn method for a csvwriter.










share|improve this question

























  • Are there any other columns?

    – ShadowRanger
    Nov 24 '18 at 1:48











  • @ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

    – PaperRockBazooka
    Dec 2 '18 at 0:27
















2















Currently I have a populated list



list =[a, b, c, d]


I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:



with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)


The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.



I have tried making a for loop to write each element + n but the writerow method has an issue with that (puts commas after each letter), and there is no writecolumn method for a csvwriter.










share|improve this question

























  • Are there any other columns?

    – ShadowRanger
    Nov 24 '18 at 1:48











  • @ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

    – PaperRockBazooka
    Dec 2 '18 at 0:27














2












2








2








Currently I have a populated list



list =[a, b, c, d]


I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:



with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)


The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.



I have tried making a for loop to write each element + n but the writerow method has an issue with that (puts commas after each letter), and there is no writecolumn method for a csvwriter.










share|improve this question
















Currently I have a populated list



list =[a, b, c, d]


I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:



with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)


The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.



I have tried making a for loop to write each element + n but the writerow method has an issue with that (puts commas after each letter), and there is no writecolumn method for a csvwriter.







python list csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 23:45









Cedric Zoppolo

1,34211327




1,34211327










asked Nov 24 '18 at 1:27









PaperRockBazookaPaperRockBazooka

476




476













  • Are there any other columns?

    – ShadowRanger
    Nov 24 '18 at 1:48











  • @ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

    – PaperRockBazooka
    Dec 2 '18 at 0:27



















  • Are there any other columns?

    – ShadowRanger
    Nov 24 '18 at 1:48











  • @ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

    – PaperRockBazooka
    Dec 2 '18 at 0:27

















Are there any other columns?

– ShadowRanger
Nov 24 '18 at 1:48





Are there any other columns?

– ShadowRanger
Nov 24 '18 at 1:48













@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

– PaperRockBazooka
Dec 2 '18 at 0:27





@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas

– PaperRockBazooka
Dec 2 '18 at 0:27












4 Answers
4






active

oldest

votes


















2














To perform this task I would use pandas package as follows:



import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")


pandas is also great to read csv and even work with Excel files.






share|improve this answer


























  • I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

    – PaperRockBazooka
    Dec 2 '18 at 0:26



















1














This is about the simplest way I can think of to do it:



import csv

my_list = ['a', 'b', 'c', 'd']

with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)


Note I changed the name of your list variable to my_list so it wouldn't conflict with the built-in list class.






share|improve this answer































    0














    You can do this by joining the elements of your list into a single string with new line characters 'nr' as the separators, then write the whole string to your file.



    For example:



    my_list = [a, b, c, d]

    with open("twitter3.csv", "w+") as csvfile:
    to_write = "nr".join(my_list)
    csvfile.write(to_write)


    (Also 'n' works)






    share|improve this answer































      -1














      I put all the code in one place, so that can copy and test it out.



      import csv

      # Should not use 'list' as variable
      lst =['a', 'b', 'c', 'd']

      # newline='' prevent additional new lines in file
      with open('twitter3.csv', 'w+', newline='') as csvfile:
      writer = csv.writer(csvfile, dialect='excel')
      for l in lst:
      writer.writerow(l)





      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%2f53454409%2fhow-to-turn-a-list-into-a-column-in-a-csv%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        To perform this task I would use pandas package as follows:



        import pandas as pd
        l=["a","b","c","d"]
        df=pd.DataFrame(l,index=False,header=False)
        df.to_csv("twitter3.csv")


        pandas is also great to read csv and even work with Excel files.






        share|improve this answer


























        • I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

          – PaperRockBazooka
          Dec 2 '18 at 0:26
















        2














        To perform this task I would use pandas package as follows:



        import pandas as pd
        l=["a","b","c","d"]
        df=pd.DataFrame(l,index=False,header=False)
        df.to_csv("twitter3.csv")


        pandas is also great to read csv and even work with Excel files.






        share|improve this answer


























        • I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

          – PaperRockBazooka
          Dec 2 '18 at 0:26














        2












        2








        2







        To perform this task I would use pandas package as follows:



        import pandas as pd
        l=["a","b","c","d"]
        df=pd.DataFrame(l,index=False,header=False)
        df.to_csv("twitter3.csv")


        pandas is also great to read csv and even work with Excel files.






        share|improve this answer















        To perform this task I would use pandas package as follows:



        import pandas as pd
        l=["a","b","c","d"]
        df=pd.DataFrame(l,index=False,header=False)
        df.to_csv("twitter3.csv")


        pandas is also great to read csv and even work with Excel files.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 2:47

























        answered Nov 24 '18 at 2:39









        Cedric ZoppoloCedric Zoppolo

        1,34211327




        1,34211327













        • I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

          – PaperRockBazooka
          Dec 2 '18 at 0:26



















        • I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

          – PaperRockBazooka
          Dec 2 '18 at 0:26

















        I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

        – PaperRockBazooka
        Dec 2 '18 at 0:26





        I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!

        – PaperRockBazooka
        Dec 2 '18 at 0:26













        1














        This is about the simplest way I can think of to do it:



        import csv

        my_list = ['a', 'b', 'c', 'd']

        with open('twitter3.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile, dialect='excel')
        writer.writerows(tuple(item) for item in my_list)


        Note I changed the name of your list variable to my_list so it wouldn't conflict with the built-in list class.






        share|improve this answer




























          1














          This is about the simplest way I can think of to do it:



          import csv

          my_list = ['a', 'b', 'c', 'd']

          with open('twitter3.csv', 'w', newline='') as csvfile:
          writer = csv.writer(csvfile, dialect='excel')
          writer.writerows(tuple(item) for item in my_list)


          Note I changed the name of your list variable to my_list so it wouldn't conflict with the built-in list class.






          share|improve this answer


























            1












            1








            1







            This is about the simplest way I can think of to do it:



            import csv

            my_list = ['a', 'b', 'c', 'd']

            with open('twitter3.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, dialect='excel')
            writer.writerows(tuple(item) for item in my_list)


            Note I changed the name of your list variable to my_list so it wouldn't conflict with the built-in list class.






            share|improve this answer













            This is about the simplest way I can think of to do it:



            import csv

            my_list = ['a', 'b', 'c', 'd']

            with open('twitter3.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, dialect='excel')
            writer.writerows(tuple(item) for item in my_list)


            Note I changed the name of your list variable to my_list so it wouldn't conflict with the built-in list class.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 2:04









            martineaumartineau

            66.2k989178




            66.2k989178























                0














                You can do this by joining the elements of your list into a single string with new line characters 'nr' as the separators, then write the whole string to your file.



                For example:



                my_list = [a, b, c, d]

                with open("twitter3.csv", "w+") as csvfile:
                to_write = "nr".join(my_list)
                csvfile.write(to_write)


                (Also 'n' works)






                share|improve this answer




























                  0














                  You can do this by joining the elements of your list into a single string with new line characters 'nr' as the separators, then write the whole string to your file.



                  For example:



                  my_list = [a, b, c, d]

                  with open("twitter3.csv", "w+") as csvfile:
                  to_write = "nr".join(my_list)
                  csvfile.write(to_write)


                  (Also 'n' works)






                  share|improve this answer


























                    0












                    0








                    0







                    You can do this by joining the elements of your list into a single string with new line characters 'nr' as the separators, then write the whole string to your file.



                    For example:



                    my_list = [a, b, c, d]

                    with open("twitter3.csv", "w+") as csvfile:
                    to_write = "nr".join(my_list)
                    csvfile.write(to_write)


                    (Also 'n' works)






                    share|improve this answer













                    You can do this by joining the elements of your list into a single string with new line characters 'nr' as the separators, then write the whole string to your file.



                    For example:



                    my_list = [a, b, c, d]

                    with open("twitter3.csv", "w+") as csvfile:
                    to_write = "nr".join(my_list)
                    csvfile.write(to_write)


                    (Also 'n' works)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 1:39









                    Henry WoodyHenry Woody

                    3,9692824




                    3,9692824























                        -1














                        I put all the code in one place, so that can copy and test it out.



                        import csv

                        # Should not use 'list' as variable
                        lst =['a', 'b', 'c', 'd']

                        # newline='' prevent additional new lines in file
                        with open('twitter3.csv', 'w+', newline='') as csvfile:
                        writer = csv.writer(csvfile, dialect='excel')
                        for l in lst:
                        writer.writerow(l)





                        share|improve this answer




























                          -1














                          I put all the code in one place, so that can copy and test it out.



                          import csv

                          # Should not use 'list' as variable
                          lst =['a', 'b', 'c', 'd']

                          # newline='' prevent additional new lines in file
                          with open('twitter3.csv', 'w+', newline='') as csvfile:
                          writer = csv.writer(csvfile, dialect='excel')
                          for l in lst:
                          writer.writerow(l)





                          share|improve this answer


























                            -1












                            -1








                            -1







                            I put all the code in one place, so that can copy and test it out.



                            import csv

                            # Should not use 'list' as variable
                            lst =['a', 'b', 'c', 'd']

                            # newline='' prevent additional new lines in file
                            with open('twitter3.csv', 'w+', newline='') as csvfile:
                            writer = csv.writer(csvfile, dialect='excel')
                            for l in lst:
                            writer.writerow(l)





                            share|improve this answer













                            I put all the code in one place, so that can copy and test it out.



                            import csv

                            # Should not use 'list' as variable
                            lst =['a', 'b', 'c', 'd']

                            # newline='' prevent additional new lines in file
                            with open('twitter3.csv', 'w+', newline='') as csvfile:
                            writer = csv.writer(csvfile, dialect='excel')
                            for l in lst:
                            writer.writerow(l)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 1:41









                            yoonghmyoonghm

                            1,076918




                            1,076918






























                                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%2f53454409%2fhow-to-turn-a-list-into-a-column-in-a-csv%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

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks