0

I am searching how to append a new row to CSV in Python. What I want is that the path to be predefined as a string object. I have come across this:

import csv   
fields=['first','second','third']
with open(r'name', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(fields)

what I want is something like this in modified in the above implementation

path='home/user/new.csv'
import csv   
fields=['first','second','third']
with open(rpath , 'a') as f:
    writer = csv.writer(f)
    writer.writerow(fields)

I can't seem to do it because of the letter r appearing in argument besides path. Can anybody guide on how to do it?

Osama Dar
  • 404
  • 5
  • 15

3 Answers3

1

In the original snippet you referred, the r prefix tells python to treat the string as a raw string and leave the backslashes in the string. (Refer https://docs.python.org/2/reference/lexical_analysis.html#string-literals for a detailed explanation)

So r can be prefixed only before a string literal like this r'somestring'.

In your example, you are defining the variable name as path. But later you are calling it as rpath. rpath is not defined anywhere in your code. So it will throw an error. You can change it to

with open(path , 'a') as f:
suryasankar
  • 4,821
  • 2
  • 14
  • 13
1

Lets suppose below is your new.csv file and you want to add a row new_row as a fourth row the CSV :

$ cat  new.csv
SN, Num1, Num2
1, one, two
2, three, four
3, five, six

Below code will do that..

$ cat appendCsv2.py
#!/isr/bin/python3
import csv

new_row =['4', 'first','second','third']
with open('new.csv', 'a') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerow(new_row)
csvFile.close()

Output desired:

$ cat new.csv
SN, Num1, Num2
1, one, two
2, three, four
3, five, six
4,first,second,third

Don't forget to close the file!

From Python Document It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:

>>> with open('workfile', 'r') as f:
...     read_data = f.read()
>>> f.closed
True
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53
  • Another question; I thought if we are using _with open_ , it is not required to close the file as it automatically closes after exiting the indentation? – Osama Dar Oct 01 '18 at 08:00
  • @Osama, agreed, Within the with statement the __enter__ method on open(...) is called and as soon as you go out of that block the `__exit__` method is called `(self.close())`. As for the f.close() after, it's not wrong but useless. It's already closed so it won't do anything. – Karn Kumar Oct 01 '18 at 08:13
0

Do you mean this?

import csv

path='/home/user/new.csv'
fields=['first','second','third']
# To prevent newline by writerow(), open with newline=""
with open(path , 'a', newline="") as f:
  writer = csv.writer(f)
  writer.writerow(fields)
yoonghm
  • 4,198
  • 1
  • 32
  • 48