3

I'm trying to write the elements in my dictionary into a text file where each key would be a column. Currently have I something that looks like

import csv
import numpy as np



data1 = np.arange(10)
data2 = np.arange(10)*2
data3 = np.arange(10)*3

writefile = '../Desktop/data.txt'
datadict = {} 

datadict['data1'] = data1
datadict['data2'] = data2
datadict['data3'] = data3


f = open( writefile, 'w' )
fieldnames = ['data1','data2', 'data3']
data = csv.DictWriter(writefile, fieldnames, restval='', extrasaction='ignore', dialect='excel')

f.close()

but it gives me the error "argument 1 must have a "write" method". I'm not sure what that means. I'm also worried about the dialect = 'excel', but I'm not sure what else to put. In the end I'd like a file that has something looking like:

enter image description here

Thanks

LondonRob
  • 73,083
  • 37
  • 144
  • 201
Alexa Halford
  • 1,173
  • 2
  • 12
  • 13

5 Answers5

8

No need to use DictWriter here at all:

import csv
import numpy as np

data1 = np.arange(10)
data2 = np.arange(10)*2
data3 = np.arange(10)*3

writefile = '../test.csv'
fieldnames = ['data1','data2', 'data3']
with open( writefile, 'w' ) as f:
    writer = csv.writer(f)
    writer.writerow(fieldnames)
    writer.writerows(zip(data1, data2, data3))
Michael
  • 7,316
  • 1
  • 37
  • 63
2

You should pass the file-like object as the first argument to the DictWriter constructor.

datalist = [{'data1': d1, 'data2': d2, 'data3': d3} for d1, d2, d3 in zip(data1, data2, data3)]
...
f = open(writefile, 'w')
...
writer = csv.DictWriter(f, ...) 
for i in xrange(10):
    writer.writerow(datalist[i])
f.close()

More simple analogic case:

with open(writefile, 'w') as f:
    writer = csv.writer(f)
    for row in zip(data1, data2, data3):
        writer.writerow(row)
defuz
  • 26,721
  • 10
  • 38
  • 60
1

If you're doing the kind of data analysis it looks like you might be doing here, the best bet might be to get into pandas, Python's dedicated data analysis library.

Here's an example which does what you want:

import pandas as pd
import numpy as np

data1 = np.arange(10)
data2 = np.arange(10)*2
data3 = np.arange(10)*3

df = pd.DataFrame(zip(data1, data2, data3), columns=['data1', 'data2', 'data3'])
df.to_csv('myfile.csv')

Pretty neat I'd say. Additionally, you can now do all sorts of unimaginable things with your data.

You can also make a DataFrame from an existing dictionary:

In [115]: a_dict = {'foo':[1,2,3], 'bar':[4,5,6], 'baz':[7,8,9]}

In [116]: pd.DataFrame(a_dict)
Out[116]: 
   bar  baz  foo
0    4    7    1
1    5    8    2
2    6    9    3
Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
0

Try this:

import csv
import numpy as np

data1 = np.arange(10)
data2 = np.arange(10)*2
data3 = np.arange(10)*3

writefile = '../Desktop/data.txt'
datadictlist = [{'data1': data1[i], 'data2': data2[i], 'data3': data3[i]} for i in xrange(10)]

f = open( writefile, 'w' )
fieldnames = ['data1','data2', 'data3']
writer = csv.DictWriter(f, fieldnames, restval='', extrasaction='ignore', dialect='excel')
writer.writerows(datadictlist)

f.close()
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
  • That just gives me an empty file without any data written in it. – Alexa Halford Oct 01 '12 at 20:57
  • It's now printing data (yea), but not in the right format. It's giving me one line with [0 1 2 3 4 5 6 7 8 9],[ 0 2 4 6 8 10 12 14 16 18],[ 0 3 6 9 12 15 18 21 24 27] – Alexa Halford Oct 01 '12 at 21:07
  • @AlexaHalford, that is because you are assigning the whole array to a single field in one dictionary. I updated the code to print the output you want to show. – Abhinav Sarkar Oct 01 '12 at 21:14
0

put 'a+' instead of 'w'

f = open( writefile, 'a+' )
andrewsi
  • 10,807
  • 132
  • 35
  • 51
nickanor
  • 637
  • 2
  • 12
  • 18