1

I have a python list as follows:

[(numpy.datetime64('2000-04-01'), 'pie'),
 (numpy.datetime64('2000-04-01'), 'apple'),
 (numpy.datetime64('2000-04-01'), 'orange'),
 (numpy.datetime64('2000-04-01'), 'mango'),
 (numpy.datetime64('2000-04-01'), 'pears')]

i want it to a csv file output as following

|    date    |  item |
+------------+-------+
| 2000-04-01 | pie   |
| 2000-04-01 | apple |
|     ...    |  ...  |
3
  • 1
    This is not a numpy object, numpy doesn't have lists. You can turn your list into a pandas' dataFrame and then save it with to_csv method. Commented Jun 4, 2018 at 19:13
  • the csv module works fine too, without pandas Commented Jun 4, 2018 at 20:15
  • Iterate on the list, format each element as you wish and write to the file. Just ordinary python text file creation. Commented Jun 4, 2018 at 20:18

2 Answers 2

1

I was able to output as csv file finally.

import csv
with open('date_prop.csv','w',newline='') as out:
    csv_out=csv.writer(out)
    csv_out.writerow(['date','item'])
    for row in f2:
        csv_out.writerow(row)
Sign up to request clarification or add additional context in comments.

Comments

1

Straight forward Python file write:

In [116]: with open('test','w') as f:
     ...:     print('   date   item', file=f)
     ...:     for t in alist:
     ...:         print('%s   %s'%t, file=f)
     ...:         
In [117]: cat test
   date   item
2000-04-01   pie
2000-04-01   apple
2000-04-01   orange
2000-04-01   mango
2000-04-01   pears

The key is that str(np.datetime64('2000-04-01') is formatted as you want.

And object dtype array can be written with savetxt with the right fmt:

In [121]: arr = np.array(alist)
In [122]: arr
Out[122]: 
array([[numpy.datetime64('2000-04-01'), 'pie'],
       [numpy.datetime64('2000-04-01'), 'apple'],
       [numpy.datetime64('2000-04-01'), 'orange'],
       [numpy.datetime64('2000-04-01'), 'mango'],
       [numpy.datetime64('2000-04-01'), 'pears']], dtype=object)
In [123]: np.savetxt('test', arr, fmt='%s', delimiter=',', header='date item')
In [124]: cat test
# date item
2000-04-01,pie
2000-04-01,apple
2000-04-01,orange
2000-04-01,mango
2000-04-01,pears

I could also create a structured array and write that with savetxt

In [125]: arr = np.array(alist, dtype='datetime64[D], U10')
In [126]: arr
Out[126]: 
array([('2000-04-01', 'pie'), ('2000-04-01', 'apple'),
       ('2000-04-01', 'orange'), ('2000-04-01', 'mango'),
       ('2000-04-01', 'pears')], dtype=[('f0', '<M8[D]'), ('f1', '<U10')])
In [127]: np.savetxt('test', arr, fmt='%s', delimiter=',  ', header='date   item
     ...: ')
In [128]: cat test
# date   item
2000-04-01,  pie
2000-04-01,  apple
2000-04-01,  orange
2000-04-01,  mango
2000-04-01,  pears

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.