Search code examples
pythoncsvfile-writing

How to write to a .csv file without "import csv"


For an assignment I have to write some data to a .csv file. I have an implementation that works using Python's csv module, but apparently I am not supposed to use any imported libraries...

So, my question is how I could go about doing so? I am no expert when it comes to these things, so I am finding it difficult to find a solution online; everywhere I look import csv is being used.


Solution

  • Since csv stands for comma-separated values, you can create a file with the regular I/O built-in function that ends with .csv like:

    f = open("demofile.csv", "w")
    

    And then write to it:

    f.write("1, 2, 3 ,4, 5\n 6, 7, 8, 9, 10")
    

    Where each cell is separated by comma, and each row is separated by \n.

    The result will look like this in MS Excel:

    enter image description here