How to make python use double quotes while writing a string to a file

file-io, python, string

Solution

Since the expected output is valid JSON, you can try:

import json

L = [[u'2014-12-02', 727.75], [u'2014-12-01', 733.65]]
with open("outfile", "w") as fdesc:
    json.dump(L, fdesc)

You might want to add `fdesc.write('\n')` after the `json.dump()` call.

Problem

I have a list in python ``` L = [[u'2014-12-02', 727.75], [u'2014-12-01', 733.65]] ``` to be written to a text file. I want the file to contain ``` [["2014-12-02", 727.75], ["2014-12-01", 733.65]] ``` if I write `file.write(str(L))` [['2014-12-02', 727.75], ['2014-12-01', 733.65]] will be written to the file.

Original source