简体   繁体   English

永久更改变量

[英]Change variable permanently

I'm writing a small program that helps you keep track of what page you're on in your books. 我正在写一个小程序,可以帮助您跟踪书中的页面。 I'm not a fan of bookmarks, so I thought "What if I could create a program that would take user input, and then display the number or string of text that they wrote, which in this case would be the page number they're on, and allow them to change it whenever they need to?" 我不喜欢书签,所以我想“如果我可以创建一个可以接受用户输入的程序,然后显示他们编写的文本的数量或字符串,在这种情况下将是他们的页码,该怎么办?”重新开始,让他们随时改变它?“ It would only take a few lines of code, but the problem is, how can I get it to display the same number the next time I open the program? 它只需要几行代码,但问题是,如何在下次打开程序时显示相同的数字? The variables would reset, would they not? 变量会重置,不是吗? Is there a way to permanently change a variable in this way? 有没有办法以这种方式永久改变变量?

You can store the values in a file, and then load them at start up. 您可以将值存储在文件中,然后在启动时加载它们。

The code would look somewhat like this 代码看起来有点像这样

variable1 = "fi" #start the variable, they can come from the main program instead
variable2 = 2

datatowrite = str(variable1) + "\n" + str(variable2) #converts all the variables to string and packs them together broken apart by a new line

f = file("/file.txt",'w')
f.write(datatowrite) #Writes the packed variable to the file
f.close() #Closes the file !IMPORTANT TO DO!

The Code to read the data would be: 读取数据的守则是:

import string

f = file("/file.txt",'r') #Opens the file
data = f.read() #reads the file into data
if not len(data) > 4: #Checks if anything is in the file, if not creates the variables (doesn't have to be four)

    variable1 = "fi"
    variable2 = 2
else:
    data = string.split(data,"\n") #Splits up the data in the file with the new line and removes the new line
    variable1 = data[0] #the first part of the split
    variable2 = int(data[1]) #Converts second part of strip to the type needed

Bear in mind with this method the variable file is stored in plaintext with the application. 请记住,使用此方法,变量文件与应用程序一起存储在明文中。 Any user could edit the variables and change the programs behaviour 任何用户都可以编辑变量并更改程序行为

You need to store it on disk. 您需要将其存储在磁盘上。 Unless you want to be really fancy, you can just use something like CSV, JSON, or YAML to make structured data easier. 除非你想要真正的幻想,否则你可以使用像CSV,JSON或YAML这样的东西来简化结构化数据。

Also check out the python pickle module. 还可以查看python pickle模块。

Variables have several lifetimes: 变量有几个生命周期:

  • If they're inside of a block of code, their value only exists for that block of code. 如果它们位于代码块内,则它们的值仅存在于该代码块中。 This covers functions, loops, and conditionals. 这包括函数,循环和条件。
  • If they're inside of an object, their value only exists for the life of that object. 如果它们位于对象内部,则它们的值仅存在于该对象的生命周期中。
  • If the object is dereferenced, or you leave your code of block early, the value of the variable is lost. 如果对象被解除引用,或者您提前保留了块代码,则该变量的值将丢失。

If you want to maintain the value of something in particular, you have to persist it. 如果你想特别保留某些东西的价值,你必须坚持下去 Persistence allows you to write a variable out to disk (and yes, databases are technically out-to-disk), and retrieve it at a later time - after the lifetime of the variable has expired, or when the program is restarted. 持久性允许您将变量写入磁盘(是的,数据库在技术上是磁盘外),并在以后检索它 - 在变量的生命周期到期后,或者重新启动程序时。

You've got several options for how you want to persist the location of their page - a heavy-handed approach would be to use SQLite ; 你有多种选择可以保持页面的位置 - 一个严厉的方法是使用SQLite ; a slightly less heavy handed approach would be to unpickle the object, or simply write to a text file. 一个稍微不那么重手的方法是unpickle的对象,或者干脆写一个文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM