python变量输出到文件_使用函数将多个变量写入文件
首先,要獲得當(dāng)前正在執(zhí)行的腳本名,或者更確切地說是調(diào)用函數(shù)的模塊,必須從堆棧跟蹤中獲取它。globals()-它將在writeToValues()函數(shù)的相同上下文中執(zhí)行,因此它不會(huì)從“調(diào)用者”接收globals()。要糾正這種情況,您可以使用inspect模塊:import inspect
import os
def writeToValues(name):
caller = inspect.getmodule(inspect.stack()[1][0])
caller_globals = caller.__dict__ # use this instead of globals()
fileName = os.path.splitext(os.path.basename(caller.__file__))[0]
# etc.
這將確保您獲得導(dǎo)入腳本并在其中調(diào)用writeToValues()的模塊的名稱。在
請記住,如果您打算編寫可用的Python文件,這是一個(gè)非常糟糕的主意-如果您的腳本名有空格(如您的示例中所示),它將寫入一個(gè)帶有空格的變量名,如果您試圖將生成的文件加載到Python解釋器中,這將進(jìn)一步導(dǎo)致語法錯(cuò)誤。在
第二,為什么在所有事物的名字fluffy你試圖做一個(gè)反向查找來找到一個(gè)變量名?您知道:
^{pr2}$
將寫入{"ab": 2},而不是{}使其在意圖(保存錯(cuò)誤的變量)和狀態(tài)表示(保存錯(cuò)誤的值)中都不正確,對嗎?您應(yīng)該傳遞一個(gè)要存儲(chǔ)/更新的變量名稱,以確保獲取正確的屬性。在
更新部分的問題更大——您需要更新文件,而不僅僅是附加到文件中。這意味著您需要找到當(dāng)前腳本的行,將其刪除,然后在其位置編寫一個(gè)同名的新dict。如果您不希望您的文件增長到很大的比例(也就是說,您可以將其部分保存在工作內(nèi)存中),您可以使用以下方法來實(shí)現(xiàn):import os
import inspect
def writeToValues(name):
caller = inspect.getmodule(inspect.stack()[1][0])
caller_globals = caller.__dict__ # use this instead of globals()
caller_name = os.path.splitext(os.path.basename(caller.__file__))[0]
# keep 'mydict' list in the caller space so multiple callers can use this
target_dict = caller_globals['mydict'] = caller_globals.get('mydict', {})
if name not in caller_globals: # the updated value no longer exists, remove it
target_dict.pop(name, None)
else:
target_dict[name] = caller_globals[name]
# update the 'values.py':
# optionaly check if you should update - if values didn't change no need for slow I/O
with open("values.py", "a+") as f:
last_pos = 0 # keep the last non-update position
while True:
line = f.readline() # we need to use readline() for tell() accuracy
if not line or line.startswith(caller_name): # break at the matching line or EOF
break
last_pos = f.tell() # new non-update position
append_data = f.readlines() # store in memory the rest of the file content, if any
f.seek(last_pos) # rewind to the last non-update position
f.truncate() # truncate the rest of the file
f.write("".join((caller_name, " = ", str(target_dict), "\n"))) # write updated dict
if append_data: # write back the rest of the file, if truncated
f.writelines(append_data)
return target_dict
否則,使用臨時(shí)文件在讀取時(shí)寫入所有內(nèi)容,除了與當(dāng)前腳本匹配的行外,為當(dāng)前腳本附加新值,刪除原始值并將臨時(shí)文件重命名為values.py。在
所以現(xiàn)在,如果您將上述內(nèi)容存儲(chǔ)在value_writter.py中,并在腳本my_script.py中使用它:import value_writter
a = 2
b = 3
value_writter.write_to_values("a")
value_writter.write_to_values("b")
a = 5
value_writter.write_to_values("a")
# values.py contains: my_script = {"a": 5, "b": 3}
對于您導(dǎo)入到的任何腳本也應(yīng)該如此。現(xiàn)在,讓多個(gè)腳本在沒有鎖定機(jī)制的情況下編輯同一個(gè)文件是一個(gè)等待發(fā)生的意外,但那是另一回事。在
此外,如果您的值很復(fù)雜,系統(tǒng)將中斷(或者更確切地說,您的dict打印輸出看起來不太合適)。幫你自己一個(gè)忙,使用一些適當(dāng)?shù)男蛄谢?#xff0c;甚至可怕的pickle也比這更好。在
總結(jié)
以上是生活随笔為你收集整理的python变量输出到文件_使用函数将多个变量写入文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL中的left outer join
- 下一篇: python 浮点数最小值_PYTHON