0

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

Arton Dorneles
  • 1,629
  • 14
  • 18
JonasT
  • 1
  • 3
  • You have a missing closing `]` at the end of your definition of `data`. I believe you should get a syntax error? – Lisa Feb 17 '16 at 14:35
  • 2
    Without the quotation marks, your last line would incorrectly be treated as containing two fields with values `Last[1` and `1];1.5;`. – chepner Feb 17 '16 at 14:41

2 Answers2

1

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.

Pedru
  • 1,430
  • 1
  • 14
  • 32
0

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!

JonasT
  • 1
  • 3