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

Re: saving variables to a file in python 80 80

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

Re: saving variables to a file in python 80 80

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

Code example here, documentation here.

Re: saving variables to a file in python 80 80

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

Re: saving variables to a file in python 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.