3

I have a code for generating a csv file shown below.

import csv

csvData = [["HAI", "Hello"]]
while(1):
    with open('test.csv', 'a') as csvFile:
        writer = csv.writer(csvFile, delimiter=',', quoting=csv.QUOTE_ALL)
        writer.writerows(csvData)
    csvFile.close()

When i run this code it will generate a csv file like this

1."HAI","Hello"

3."HAI","Hello"

That is it will generate on alternative lines,that is first write takes palce on line 1 ,second write takes on line 3.I want to write it on line 2. Anybody please help me.

1
  • You can try using the approach specified in my answer below. I tested it with your minimal working example, and it produced the correct output for me. Commented Jun 19, 2018 at 6:13

1 Answer 1

2

If you look at the documentation for CSV formatting parameters, you will see that there is the argument lineterminator, which defaults to \r\n.

You can get your desired output by simply passing a regular \n like so:

writer = csv.writer(csvFile, delimiter=",", quoting=csv.QUOTE_ALL, lineterminator="\n")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.