954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

saving variables to a file in python

Hello all,
i want to save the variables present in python workspace to text file.I have used file.write() option but it converts the variables(and also arrays) to string.I want to store them as they are present in IDLE.

Is it possible?

Thanking all in anticipation.

regards
wajih

wajih
Newbie Poster
1 post since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

I think pickle is what you want. It's a core module, so you can just import pickle. Code example here , documentation here .

Jackson William
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 3
 

@ Jackson William:
You had formatted the links incorrectly (forgotten http://) . Fix:

Code example here , documentation here .

Phelerox
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

Also what Data type of those variables?
If they are numeric, then Just save them as strings. Then retrieve them and use int() or float() to convert them back o numerical values

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

Jackson William is correct, module pickle is used to save the object value and type.

Another way to do it is to save the code as a module, then use the module namespace to retrieve the variable.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You