Saving variable value outside of python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to python, been coding in school for about a year now, but I like to code when I get bored. I have made two programs but both are useless as I have to input each value of the variables every time I start it up. Is there anyway I can save the value of a variable externally so when it loads it will open up the file and assign each variable?

You should use the pickle module for that purpose:
l =[1,2,3,4,5]
import pickle
pickle.dump(l,open("mydata","wb"))
and for getting your variable back:
import pickle
l = pickle.load(open("mydata","rb"))
If you have many variables to save, consider embedding them in a dictionary for instance.

Yo can use the shelve module its pretty simple it puts all variables into a dictionary then when your file reopens you can make shelve set the variables back. Here is a good example of using the shelve module.

To save data to a file, you could use
filehandle = open(filename, 'w')
filehandle.write(string)
filehandle.close()
Preferred in Python is
with open(filename, 'w') as filehandle:
filehandle.write(string)
because the file will be closed upon exiting the with block even if the block exits with an error, and without requiring the programmer to remember to close the file.
Load the values back in with filehandle.readline() or readlines().
You can also use the Python libraries json or csv to facilitate moving data into and out of files. If you have no need to inspect or modify the data in the file using another program (e.g. Notepad++ or MS Excel), you might prefer pickle or shelve.

Related

Save and load game minesweeper [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I need help. The task is to add to this game (minesweeper), save the game and download from a file (if there is no file, you need to create one). Also add a check box counter.
My code
You could use pickle for serializing the current instance of the class that is executing the game like:
import pickle
def save(self, filename):
with open(filename, mode) as f:
pickle.#dump, load(self, f)
for mode insert either wb or rb for writing and reading, and then one line below .dump for saving and .load for loading.
Then whenever needed you just call the functions.
I suggest you just read the pickle docs to implement it, so you can acutally understand the logic behind it, instead of blind-copying.
Edit: As Bryan Oakley correctly mentioned, you cant pickle tkinter objects, so for them I would recommend storing the state of the tkinter objects in a dictionary in the class, and pickle that dictionary instead of the tkinter objects.

Can I use a text file to load data into a dictionary in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Basically I want to store a dictionary in a text file, the reason for this is I want to be able to open the file and edit it even after I finalise the program.
It's a quiz program I'm using to help me learn the dates of certain events in geological history and I want to be able to add more questions without accessing the code.
So I would want a list of questions with their dates next to them that would be imported back to python, basically a file with a bunch of questions like this:
"Pangea formed":335000000, #Carboniferous
"Gondwana split into Africa and South America":180000000, #Jurassic
"The Chicxulub crater, buried in Mexico, formed":66043000, #Cretaceous
The problem is I don't think python is made to tell the difference from the string and the value when reading a notepad file.
Is there a way to do this?
You want to load the content of a dictionary from a file. The file should be human readable/editable. One solution is to keep the file in JSON format.
JSON is a format that can be used to convert basic objects like dictionaries, lists into text and vice versa.
So let the content of the file be:
{
"Pangea formed":335000000,
"Gondwana split into Africa and South America":180000000,
"The Chicxulub crater, buried in Mexico, formed":66043000
}
Now the file, even tough is a text file that you can open and edit in Notepad, is in the JSON format. Notice the opening and closing braces. To load the file into a dictionary you can do:
import json
with open('file.json') as f:
my_dict = json.load(f)
Note that I had removed the comments as JSON doesn't support comments. Also, the last key-value pair doesn't end with a comma.
Yes this is possible. Take a look at ConfigParser. It works based on textfiles with the .ini extension.

Python with large files slower than perl: what am I doing wrong? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm increasingly using python instead of perl but have one problem: always when I want to process large files (>1GB) line by line python seems to take ages for a job that perl does in a fraction of the time. However, the general opinion on the web seems to be that python should be at least as fast for text processing as perl. So my question is what am I doing wrong?
Example:
Read a file line by line, split the line at every tab and add the second item to a list.
My python solution would look something like this:
with open() as infile:
for line in infile:
ls = line.split("\t")
list.append(ls[1])
The perl code would look like this:
open(my $infile,"<",file_path);
while(my $line=<$infile>){
my #ls = split(/\t/,$line);
push #list, $ls[1]
}
close($infile)
Is there any way to speed this up?
And to make it clear: I don't want to start the usual "[fill in name of script language A] is sooo much better than [fill in script language B]" thread. I'd like to use python more by this is a real problem for my work.
Any suggestions?
Is there any way to speed this up?
Yes, import the CSV in SQLite and process it there. In your case you want .mode tabs instead of .mode csv.
Using any programming language to manipulate a CSV file is going to be slow. CSV is a data transfer format, not a data storage format. They'll always be slow and unwieldy to work with because you're constantly reparsing and reprocessing the CSV file.
Importing it into SQLite will put it into a much, much more efficient data format with indexing. It will take about as much time as Python would, but only has to be done once. It can be processed using SQL, which means less code to write, debug, and maintain.
See Sqlite vs CSV file manipulation performance.

Python memory leak: do I need to delete? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a python script that is slowly consuming all of my memory (48GB). If I recall, python will perform garbage collection so there is no need for me to cleanup after myself?
for example:
class data_store:
dat1={}
dat2={}
dat3={}
class myclass ():
def mem_func(self):
self.x = data_store()
self.x.dat1 = (lots of data)
self.x.dat2 = (lots of data)
y = x.dat1 + 1
...
Most of my data is stored in data_store() temporarily before it is written out to files. I would think that this would be the source of the leak. Everytime mem_func() is called a new data_store() object is created and assigned to self.x. I assume that the old data_store() object would now be a candidate for the GC to delete. In addition, I would assume that y also be able to be deleted after mem_func completes.
The only other thing I can think of is that I am creating figures with matplotlib and saving them to a file. That is all done in one function but perhaps I need to delete the figure properly. Also, I have a sqlite db that is open the whole time where I am writing data but that is not alot of data. The image is much bigger.
You need to remember that GC only collects data that no pointer (variable) is pointing at it. In other words, as long as the memory is accessible via your variables, it won't be collected/freed.
So you need to assing None to the variables you don't need any more, or assign new data to the same variable names, if you don't need them any more.

Phonebook python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi i am new to python an di recently created a phonebook using the dictionary function and then changed into exe using py2exe. I am facing a problem now i enter names in to the phone book and then when i exit the program and return back all the numbers are gone. SO is there any way to save the names and numbers entered into the program? Please give me the code as i am doing this for my class and they would be mad if the numbers disappeared everytime they exited the phonebook! PLEASE HELP!
If you do not yet want to learn relational databases, NoSQL or cloud solutions, you can start by using shelve module.
Well, the main problem is that you do store new values in your database, which is in this case represented with dict, but your don't save it's condition between scripts executions. The time of existing of an object in your script - while the the script is running by interpreter and an object has some links to it. When your restart your program - you're starting to run your script all over again, and it stores in the dict only the elements, which were specified during the script.
The most simple solution, in my opinion, is to use python pickle module. You're gonna save that dict in a file and then load it in the begging of your scrip and save it at the end.
you need to update script code with something like this:
default = {'Sarah': 7736815441,
'John': 7736815442}
def start():
#some code here, before you're trying to access phone numbers in your dict
try:
phonebook = pickle.load(open("data.pb", "r"))
except IOError:
phonebook = default
#your script here
def exit():
#some code here, last chance to modify your dict,
#so changes will appear in next program executions
pickle.dump(phonebook, open("data.pb", "w"))
hope you're familiar with python functions, if no - you can read about then here

Categories

Resources