日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python变量输出到文件_使用函数将多个变量写入文件

發布時間:2023/12/9 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python变量输出到文件_使用函数将多个变量写入文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,要獲得當前正在執行的腳本名,或者更確切地說是調用函數的模塊,必須從堆棧跟蹤中獲取它。globals()-它將在writeToValues()函數的相同上下文中執行,因此它不會從“調用者”接收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.

這將確保您獲得導入腳本并在其中調用writeToValues()的模塊的名稱。在

請記住,如果您打算編寫可用的Python文件,這是一個非常糟糕的主意-如果您的腳本名有空格(如您的示例中所示),它將寫入一個帶有空格的變量名,如果您試圖將生成的文件加載到Python解釋器中,這將進一步導致語法錯誤。在

第二,為什么在所有事物的名字fluffy你試圖做一個反向查找來找到一個變量名?您知道:

^{pr2}$

將寫入{"ab": 2},而不是{}使其在意圖(保存錯誤的變量)和狀態表示(保存錯誤的值)中都不正確,對嗎?您應該傳遞一個要存儲/更新的變量名稱,以確保獲取正確的屬性。在

更新部分的問題更大——您需要更新文件,而不僅僅是附加到文件中。這意味著您需要找到當前腳本的行,將其刪除,然后在其位置編寫一個同名的新dict。如果您不希望您的文件增長到很大的比例(也就是說,您可以將其部分保存在工作內存中),您可以使用以下方法來實現: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

否則,使用臨時文件在讀取時寫入所有內容,除了與當前腳本匹配的行外,為當前腳本附加新值,刪除原始值并將臨時文件重命名為values.py。在

所以現在,如果您將上述內容存儲在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}

對于您導入到的任何腳本也應該如此。現在,讓多個腳本在沒有鎖定機制的情況下編輯同一個文件是一個等待發生的意外,但那是另一回事。在

此外,如果您的值很復雜,系統將中斷(或者更確切地說,您的dict打印輸出看起來不太合適)。幫你自己一個忙,使用一些適當的序列化,甚至可怕的pickle也比這更好。在

總結

以上是生活随笔為你收集整理的python变量输出到文件_使用函数将多个变量写入文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。