Deathcore Ortega
Deathcore Ortega

Reputation: 39

How to write a csv with quotes?

I'm working with python, and also i'm working on a csv parse script, but i cannot write as CSV the new file with quotes as the input file, i access to the file in this way:

def read_file(self):
    with open(self.file) as f:
        parse_data=[row  for row in csv.reader(f, delimiter=',',quotechar=('\"'), skipinitialspace=True)]
        parse_data=self.organize_file(parse_data)
        return parse_data

and when i try to write a new CSV file to store the new file i execute this:

with open("output.txt","wb") as m:
    writer=csv.writer(m,delimiter=',',quotechar=('\n"'))
    writer.writerows(meh)

with meh as the variable who has the lists to write on the CSV file, but when i open the output.txt file i cannot see the quotes on the fields, like the original

can somebody help me to realize what's wrong with my code, or pleas help me to modify the output to have all the quotes

Thanks!!

Upvotes: 0

Views: 85

Answers (1)

Nathan
Nathan

Reputation: 153

Try ammending the csv.writer line to:

writer=csv.writer(m,delimiter=',',quotechar=('\n"'),quoting=csv.QUOTE_ALL)

See here for other quoting options.

Upvotes: 3

Related Questions