JonasT
JonasT

Reputation: 1

Unwanted quotation marks in CSV file

I have a code that looks like this:

with open('Recipe 2.txt', 'w') as fp:
    a = csv.writer(fp)
    data = [['VER:3;'],
            ['Recipe 0;'],
            ['Last[1,1];'+d+';'],
    a.writerows(data)

d is defined earlier.

My problem is that when i write this to a text file, i get quotationmarks around the last row like this:

Recipe 0;
"Last[1,1];1.5;"

How can I get rid of the quotationmarks on the last line?

Best, Jonas

Upvotes: 0

Views: 244

Answers (2)

JonasT
JonasT

Reputation: 1

Thank you for the help!

I found that it was the comma INSIDE the bracket that was creating the quotation marks. I simply edited it from: ['Last[1,1];'+d+';'], to ['Last[1];'+d+';'],

And I got rid of the quotationmarks.

Thank you!

Upvotes: 0

Pedru
Pedru

Reputation: 1450

I assume you want your CSV file to be like

VER:3
Recipe 0
Last[1,1]; <content_of_d>

your data should look something like this

data = [
    ['VER:3'],
    ['Recipe 0'],
    ['Last[1,1]', d]
]

Where each list is a row and each string in the row is a field. I guess those semicolon you put there are the separators, there is no need to insert them, the writer will do that for you. The separator it uses depends which dialect you configured it with.

Upvotes: 1

Related Questions