GSatterwhite
GSatterwhite

Reputation: 301

Formatting csv row made from dictionary

I am writing appending new dictionary entries to a csv as they are made. This works completely fine, however the formatting is funky. I am recording the trades my automated strategy makes. It records the timestamp, the trade, and the price.

Here is my function that records a new dictionary entry and appends to my csv:

def record_example(x):
    now = datetime.now()
    unix = int(mktime(now.timetuple()))
    price = 8110

    log.example[unix] = [unix, x, price]

    with open('example.csv', 'a') as e:
        writer = csv.DictWriter(e, fieldnames=log.example)
        writer.writerow(log.example)

        e.close()

I call the function w/:

record('buy')

this creates the dictionary as it should:

{1559052353: [1559052353, 'buy', 8110]}

however the entry into the csv now looks like with it only filling A1:

"[1559052353, 'buy', 8110]"

when I am trying to get it to look like: (so that I can import the csv into another .py file and plot trade performance)

1559052353, buy, 8110

so that 1559052353 will be in A1, buy in A2, and 8110 in A3 for example

I have tried ''.join(split(',') for each row in csv yet get error message.. thanks so much if you can help!

Upvotes: 0

Views: 41

Answers (3)

Thomas Kimber
Thomas Kimber

Reputation: 11107

Check out the dialects option in csv.dictwriter this allows you to set different conventions for your csv, my guess is you want QUOTE_MINIMAL.

Also, you're writing out a key, and a list to your dictwriter. Normally, the dictwriter will take a dictionary per row, with each field being lodged in the dictionary as a "fieldname":fieldvalue pair.

Try converting your output routine so it passes individual row-dictionaries like:

log.example[unix] = {"unix":unix, "x":x, "price":price}

{"unix" : 1559052353, "x" : 'buy', "price" : 8110}

The dictwriter will output a row with the 3 fields, "unix", "x", "price" according to your preferred csv dialect.

As it stands, you're giving it a key'ed list, so it's outputting that list "raw", which means it applies those unwanted extra decorations "[", "]"

Upvotes: 0

buran
buran

Reputation: 14263

with your code you get header to be 1559052353 and the value is the whole list (converted to str, that's why you get also the double quotes)

I believe you want to have fieldnames unix,x,price and values 1559052353,buy,8110

with open('example.csv', 'a') as e:
    header = ['unix', 'x', 'price']
    writer = csv.DictWriter(e, fieldnames=header)
    writer.writeheader()
    writer.writerow(dict(zip(header, log.example[unix])))

note when using with context manager you don't need to call e.close() you may want to make log.example[unix] to be a dict, not list. also, maybe replace x in header with something else

Upvotes: 1

javajav
javajav

Reputation: 379

you probably wanted to write: writer.writerow(log.example[unix]). this will do the thing you want. you also don't need to use e.close(), as the with block closes the file automatically for you.

Upvotes: 0

Related Questions