Getting unwanted “ ” between words when appending a CSV file to a new CSV file in Python 3












1















I'm trying to append a CSV file to a new CSV file, and where there were no "" between the words in my first document, i get those when appending it to the new file. Example :



house
building
apartment
loan


is how is the CSV file I want to import. I wrote this code :



import csv 

csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
csv_file_writer = csv.writer(csv_file, delimiter= ",")

document_to_append = open("test.csv", "r")
for row in document_to_append:
print(row)
csv_file_writer.writerow([row])

document_to_append.close()
csv_file.close()


And the result in CSV is this :



"house
"
"building
"


and so on…



How can I get rid of those annoying ""? I have absolutely no idea…










share|improve this question























  • I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

    – Rahul Agarwal
    Nov 14 '18 at 12:16











  • What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

    – Patol75
    Nov 14 '18 at 12:22











  • It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

    – BeatJuice
    Nov 14 '18 at 12:26
















1















I'm trying to append a CSV file to a new CSV file, and where there were no "" between the words in my first document, i get those when appending it to the new file. Example :



house
building
apartment
loan


is how is the CSV file I want to import. I wrote this code :



import csv 

csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
csv_file_writer = csv.writer(csv_file, delimiter= ",")

document_to_append = open("test.csv", "r")
for row in document_to_append:
print(row)
csv_file_writer.writerow([row])

document_to_append.close()
csv_file.close()


And the result in CSV is this :



"house
"
"building
"


and so on…



How can I get rid of those annoying ""? I have absolutely no idea…










share|improve this question























  • I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

    – Rahul Agarwal
    Nov 14 '18 at 12:16











  • What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

    – Patol75
    Nov 14 '18 at 12:22











  • It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

    – BeatJuice
    Nov 14 '18 at 12:26














1












1








1








I'm trying to append a CSV file to a new CSV file, and where there were no "" between the words in my first document, i get those when appending it to the new file. Example :



house
building
apartment
loan


is how is the CSV file I want to import. I wrote this code :



import csv 

csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
csv_file_writer = csv.writer(csv_file, delimiter= ",")

document_to_append = open("test.csv", "r")
for row in document_to_append:
print(row)
csv_file_writer.writerow([row])

document_to_append.close()
csv_file.close()


And the result in CSV is this :



"house
"
"building
"


and so on…



How can I get rid of those annoying ""? I have absolutely no idea…










share|improve this question














I'm trying to append a CSV file to a new CSV file, and where there were no "" between the words in my first document, i get those when appending it to the new file. Example :



house
building
apartment
loan


is how is the CSV file I want to import. I wrote this code :



import csv 

csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
csv_file_writer = csv.writer(csv_file, delimiter= ",")

document_to_append = open("test.csv", "r")
for row in document_to_append:
print(row)
csv_file_writer.writerow([row])

document_to_append.close()
csv_file.close()


And the result in CSV is this :



"house
"
"building
"


and so on…



How can I get rid of those annoying ""? I have absolutely no idea…







python csv python-3.7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:59









BeatJuiceBeatJuice

223




223













  • I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

    – Rahul Agarwal
    Nov 14 '18 at 12:16











  • What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

    – Patol75
    Nov 14 '18 at 12:22











  • It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

    – BeatJuice
    Nov 14 '18 at 12:26



















  • I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

    – Rahul Agarwal
    Nov 14 '18 at 12:16











  • What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

    – Patol75
    Nov 14 '18 at 12:22











  • It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

    – BeatJuice
    Nov 14 '18 at 12:26

















I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

– Rahul Agarwal
Nov 14 '18 at 12:16





I think it is the spaces and the quotes are coming to next line..try trimming spaces and try...It will work

– Rahul Agarwal
Nov 14 '18 at 12:16













What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

– Patol75
Nov 14 '18 at 12:22





What happens if you get rid of the last item of row ? csv_file_writer.writerow([row[:-1]])

– Patol75
Nov 14 '18 at 12:22













It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

– BeatJuice
Nov 14 '18 at 12:26





It works with csv_file_writer.writerow([row[:-1]]), I Don't understand why but ok. If you can explain it's perfect (for my knowledge), thanks anyway!

– BeatJuice
Nov 14 '18 at 12:26












3 Answers
3






active

oldest

votes


















0














You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE like below:



import csv
csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
csv_file_writer = csv.writer(csv_file, delimiter= ",", escapechar=' ',quoting=csv.QUOTE_NONE,lineterminator='')
document_to_append = open("test.csv", "r")
for row in document_to_append:
print(row)
csv_file_writer.writerow([row])
document_to_append.close()
csv_file.close()


Demo Link:




https://repl.it/repls/FarLopsidedBaitware







share|improve this answer

































    0














    replace the line:



    document_to_append = open("test.csv", "r")


    by:



    document_to_append = [line.rstrip() for line in open('test.csv', "r")]


    Note: this will work but you would have to remove the line: document_to_append.close() as document_to_append is no longer a handle, but a list.



    If you want to close the file anyhow, then instead of list comprehension(the above solution), do this:



    with open('test.csv') as f:
    document_to_append = [line.rstrip() for line in f]





    share|improve this answer































      0














      I'm not sure if this is the reason, but I think it's because you're turning row into a list in the line csv_file_writer.writerow([row]) I'd use this below:



      import csv 

      with open("bigcsv.csv", "r") csv_file:
      with open("test.csv", "w") as csv_file_writer:
      for row in csv_file:
      csv_file_writer.writerow(row)





      share|improve this answer


























      • If I remove the list, it's writing a coma between every letter

        – BeatJuice
        Nov 14 '18 at 12:24











      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%2f53299746%2fgetting-unwanted-between-words-when-appending-a-csv-file-to-a-new-csv-file-i%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














      You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE like below:



      import csv
      csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
      csv_file_writer = csv.writer(csv_file, delimiter= ",", escapechar=' ',quoting=csv.QUOTE_NONE,lineterminator='')
      document_to_append = open("test.csv", "r")
      for row in document_to_append:
      print(row)
      csv_file_writer.writerow([row])
      document_to_append.close()
      csv_file.close()


      Demo Link:




      https://repl.it/repls/FarLopsidedBaitware







      share|improve this answer






























        0














        You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE like below:



        import csv
        csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
        csv_file_writer = csv.writer(csv_file, delimiter= ",", escapechar=' ',quoting=csv.QUOTE_NONE,lineterminator='')
        document_to_append = open("test.csv", "r")
        for row in document_to_append:
        print(row)
        csv_file_writer.writerow([row])
        document_to_append.close()
        csv_file.close()


        Demo Link:




        https://repl.it/repls/FarLopsidedBaitware







        share|improve this answer




























          0












          0








          0







          You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE like below:



          import csv
          csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
          csv_file_writer = csv.writer(csv_file, delimiter= ",", escapechar=' ',quoting=csv.QUOTE_NONE,lineterminator='')
          document_to_append = open("test.csv", "r")
          for row in document_to_append:
          print(row)
          csv_file_writer.writerow([row])
          document_to_append.close()
          csv_file.close()


          Demo Link:




          https://repl.it/repls/FarLopsidedBaitware







          share|improve this answer















          You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE like below:



          import csv
          csv_file = open("bigcsv.csv", "a", encoding='utf-8', newline = "")
          csv_file_writer = csv.writer(csv_file, delimiter= ",", escapechar=' ',quoting=csv.QUOTE_NONE,lineterminator='')
          document_to_append = open("test.csv", "r")
          for row in document_to_append:
          print(row)
          csv_file_writer.writerow([row])
          document_to_append.close()
          csv_file.close()


          Demo Link:




          https://repl.it/repls/FarLopsidedBaitware








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 12:37

























          answered Nov 14 '18 at 12:31









          Jay Shankar GuptaJay Shankar Gupta

          5,5661422




          5,5661422

























              0














              replace the line:



              document_to_append = open("test.csv", "r")


              by:



              document_to_append = [line.rstrip() for line in open('test.csv', "r")]


              Note: this will work but you would have to remove the line: document_to_append.close() as document_to_append is no longer a handle, but a list.



              If you want to close the file anyhow, then instead of list comprehension(the above solution), do this:



              with open('test.csv') as f:
              document_to_append = [line.rstrip() for line in f]





              share|improve this answer




























                0














                replace the line:



                document_to_append = open("test.csv", "r")


                by:



                document_to_append = [line.rstrip() for line in open('test.csv', "r")]


                Note: this will work but you would have to remove the line: document_to_append.close() as document_to_append is no longer a handle, but a list.



                If you want to close the file anyhow, then instead of list comprehension(the above solution), do this:



                with open('test.csv') as f:
                document_to_append = [line.rstrip() for line in f]





                share|improve this answer


























                  0












                  0








                  0







                  replace the line:



                  document_to_append = open("test.csv", "r")


                  by:



                  document_to_append = [line.rstrip() for line in open('test.csv', "r")]


                  Note: this will work but you would have to remove the line: document_to_append.close() as document_to_append is no longer a handle, but a list.



                  If you want to close the file anyhow, then instead of list comprehension(the above solution), do this:



                  with open('test.csv') as f:
                  document_to_append = [line.rstrip() for line in f]





                  share|improve this answer













                  replace the line:



                  document_to_append = open("test.csv", "r")


                  by:



                  document_to_append = [line.rstrip() for line in open('test.csv', "r")]


                  Note: this will work but you would have to remove the line: document_to_append.close() as document_to_append is no longer a handle, but a list.



                  If you want to close the file anyhow, then instead of list comprehension(the above solution), do this:



                  with open('test.csv') as f:
                  document_to_append = [line.rstrip() for line in f]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 12:48









                  ShashankShashank

                  222111




                  222111























                      0














                      I'm not sure if this is the reason, but I think it's because you're turning row into a list in the line csv_file_writer.writerow([row]) I'd use this below:



                      import csv 

                      with open("bigcsv.csv", "r") csv_file:
                      with open("test.csv", "w") as csv_file_writer:
                      for row in csv_file:
                      csv_file_writer.writerow(row)





                      share|improve this answer


























                      • If I remove the list, it's writing a coma between every letter

                        – BeatJuice
                        Nov 14 '18 at 12:24
















                      0














                      I'm not sure if this is the reason, but I think it's because you're turning row into a list in the line csv_file_writer.writerow([row]) I'd use this below:



                      import csv 

                      with open("bigcsv.csv", "r") csv_file:
                      with open("test.csv", "w") as csv_file_writer:
                      for row in csv_file:
                      csv_file_writer.writerow(row)





                      share|improve this answer


























                      • If I remove the list, it's writing a coma between every letter

                        – BeatJuice
                        Nov 14 '18 at 12:24














                      0












                      0








                      0







                      I'm not sure if this is the reason, but I think it's because you're turning row into a list in the line csv_file_writer.writerow([row]) I'd use this below:



                      import csv 

                      with open("bigcsv.csv", "r") csv_file:
                      with open("test.csv", "w") as csv_file_writer:
                      for row in csv_file:
                      csv_file_writer.writerow(row)





                      share|improve this answer















                      I'm not sure if this is the reason, but I think it's because you're turning row into a list in the line csv_file_writer.writerow([row]) I'd use this below:



                      import csv 

                      with open("bigcsv.csv", "r") csv_file:
                      with open("test.csv", "w") as csv_file_writer:
                      for row in csv_file:
                      csv_file_writer.writerow(row)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 15 '18 at 10:29

























                      answered Nov 14 '18 at 12:21









                      JabaJaba

                      7,471175494




                      7,471175494













                      • If I remove the list, it's writing a coma between every letter

                        – BeatJuice
                        Nov 14 '18 at 12:24



















                      • If I remove the list, it's writing a coma between every letter

                        – BeatJuice
                        Nov 14 '18 at 12:24

















                      If I remove the list, it's writing a coma between every letter

                      – BeatJuice
                      Nov 14 '18 at 12:24





                      If I remove the list, it's writing a coma between every letter

                      – BeatJuice
                      Nov 14 '18 at 12:24


















                      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%2f53299746%2fgetting-unwanted-between-words-when-appending-a-csv-file-to-a-new-csv-file-i%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

                      Bressuire

                      Vorschmack

                      Quarantine