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?

0

3 Answers 3

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:
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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, 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.
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)

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.