unknown
unknown

Reputation: 343

Writing multi-dimensional list value to a file in Python

Thanks for clearing up my confusions on my previous question of saving data from file to multidimensional list here

I look up through the Internet to do the reverse, saving multidimensional list to a file. But, I didn't get any lead to compute that. If its not too much, I would appreciate if you can suggest me how could I save a multidimensional list to a file.

The list is something like

['b',30.83,0,'u','g','w','v',1.25,'t','t',01,'f','g',00202,0,+]
['a',58.67,4.46,'u','g','q','h',3.04,'t','t',06,'f','g',00043,560,+]

The format needed to be saved in the file is

b,30.83,0,u,g,w,v,1.25,t,t,01,f,g,00202,0,+
a,58.67,4.46,u,g,q,h,3.04,t,t,06,f,g,00043,560,+

Thanks for your help!!!

Upvotes: 0

Views: 4205

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251021

Use the csv module:

import csv
with open(filename, 'w') as f:
   writer = csv.writer(f, delimiter=',')
   writer.writerows(my_list)  #considering my_list is a list of lists.

Upvotes: 2

Scott Hunter
Scott Hunter

Reputation: 49873

Join each list with "," & write as a line to your file.

Upvotes: 1

Related Questions