嘗試使用我的代碼生成的值更新檔案中的字串,但不確定最好的方法是什么。
from datetime import datetime
import sys
import time
today = datetime.today().strftime('%m/%d/%Y')
a = 546
color = "#F1C40F"
replacements = {'SCORE':'{}', 'CURRDATE':'{}', 'COLORCODE':'{}'}.format(a, today, color)
with open('/home/kali/Desktop/template.txt') as infile, open('/home/kali/Desktop/updatedtemplate.txt', 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
uj5u.com熱心網友回復:
' dict ' 物件沒有屬性' format ' ,因此替換定義不正確。
如果你像這樣改變它,那就沒問題了。
replacements = {'SCORE': a, 'CURRDATE': today, 'COLORCODE': color}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510283.html
標籤:Python细绳变量
