0

I am working on a small project,where one of the step is to write on a csv file a list of data. I am looping through a list,and fetching specific data and writing it to a csv. The problem is when the write operation is being processed it removes previous entries of the csv file.My csv writing method is as follows: and an example

def csv_writer(data_list,file_path):
    import csv
    file_p = open(file_path, "wb")
    write_pattern = csv.writer(file_p, delimiter=",", quotechar=",")
    write_pattern.writerow(data_list)
    file_p.close() 

An example data list is:

data_list = ['a', '', 'a', 'a', 'd', 'e']

And I am trying to have data in the file as follows:

a,,a,d,e
a,b,c,d,e
a,g.j,l,m

Thanks in advance.

4
  • 1
    Python documentation to the rescue! docs.python.org/library/functions.html#open Commented Apr 27, 2011 at 17:15
  • Please, rephrase your sentence in order that it is understandable: " my goal is to every data_list that is returned from the loop" From your exemple, I understand that you want to insert data_list BEFORE all the content of the file. If not so, it means that you will have to do efforts to express your questions in understandable way Commented Apr 27, 2011 at 17:50
  • 1
    @eyquem I have changed it,hope it express the problem well :) Commented Apr 27, 2011 at 17:53
  • No, there is still ambiguity if the exemple means that you want to insert data_list BEFORE or AFTER the existing content Commented Apr 27, 2011 at 17:58

3 Answers 3

5

Maybe open the file in append mode?

file_p = open(file_path, "ab")
Sign up to request clarification or add additional context in comments.

1 Comment

it works!thanks alot,I was searching for this mode but unfortunately missed it, at last thought,I will have to custom method for this stuff.
0

When you open the file, try opening it for appending, like this.

def csv_writer(data_list,file_path):
    import csv
    file_p = open(file_path, "a")
    write_pattern = csv.writer(file_p, delimiter=",", quotechar=",")
    write_pattern.writerow(data_list)
    file_p.close() 

Comments

0

delimiter and quotechar must preferably be choosen as different

As I understood the question, the aim is to insert a new line before all the content of a CSV file. Here's the code to do that:

import csv

def csv_writer(data_list,file_path):
    file_p = open(file_path, "rb+")
    reader_pattern = csv.reader(file_p, delimiter=",", quotechar="$")
    write_pattern = csv.writer(file_p, delimiter=",", quotechar="$")

    content = list(reader_pattern)

    file_p.seek(0,0)
    write_pattern.writerow(data_list)
    write_pattern.writerows(content)
    file_p.truncate()
    file_p.close()


data_list = ['a', '', 'a', 'a', 'd', 'e']


csv_writer(data_list,'Copie de zozou.txt')

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.