0
number =[1,2,3]
day = 4
year = 2022
with open('notables.csv', 'w') as notables:
    file_write=csv.writer(notables, delimiter = ',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    file_write.writerow=([number, day, year])

my error when running the above code is: AttributeError: '_csv.writer' object attribute 'writerow' is read-only

I though csv.writer was used for writing? Am I not suppose to use .writerow here? What am I suppose to use?

greenik
  • 11
  • 4
  • file_write.writerow([number, day, year]) – Mahamudul Hasan Feb 15 '22 at 01:04
  • `file_write.writerow=([number, day, year])` Why are you _assigning_ to writerow? Why is that equal sign there? – John Gordon Feb 15 '22 at 01:12
  • Voting to close as a typo. Drop that `=` in `file_write.writerow=`. – Mark Tolonen Feb 15 '22 at 01:24
  • FYI add `newline=''` parameter to `open` per `csv` module documentation. See [this](https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row). And you can use `csv.writer(notables)` since the other parameters are the normal defaults. – Mark Tolonen Feb 15 '22 at 01:26

1 Answers1

0

Try removing the equals sign when you are writing the row, like this:

number =[1,2,3]
day = 4
year = 2022
with open('notables.csv', 'w') as notables:
    file_write=csv.writer(notables, delimiter = ',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    file_write.writerow([number, day, year])

Additionally, if you want to write multidimensional data, you will need to use file_write.writerows({rows}). And, if the file doesn't exist yet, you will want to change the 'w' on line 4 to 'w+', to indicate that you want to create a new file.