0

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!!!

2
  • 1
    Numbers like 00043 are automatically converted to integers, you should convert them to strings if you want to store them as is. Commented Jan 9, 2014 at 15:01
  • 2
    Most notably, 00043 == 35 in python2 and SyntaxError in python3. A leading zero indicates a octal number. Commented Jan 9, 2014 at 15:04

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.