0

I am trying to write three lines of text to a text file. The code and the resulting file is this:

import csv

def writer(data, path):
  with open(path, "w") as w_obj:
    to_write = csv.writer(w_obj, delimiter=",")
    for row in data:  
      to_write.writerow(row)


if __name__ == "__main__":
  data = ["first,last,state".split(","),
    "dim,pod,oh".split(","),
    "john,jones,mi".split(",")]

  path = "csv_data.txt"

  writer(data, path)

first,last,state

dim,pod,oh

john,jones,mi

Why does it add a newline after each row?

Also, the tutorial I am using says to use newline='' in the open function. What is it supposed to do and why is my Python 2.7 not recognizing it?

Dima
  • 459
  • 1
  • 3
  • 13

0 Answers0