我是一個使用python的新手,所以請原諒基本問題。python中的string.replace方法
我想在python中使用string.replace方法,並得到一個奇怪的行爲。這是我在做什麼:
# passing through command line a file name
with open(sys.argv[2], 'r+') as source:
content = source.readlines()
for line in content:
line = line.replace(placeholerPattern1Replace,placeholerPattern1)
#if I am printing the line here, I am getting the correct value
source.write(line.replace(placeholerPattern1Replace,placeholerPattern1))
try:
target = open('baf_boot_flash_range_test_'+subStr +'.gpj', 'w')
for line in content:
if placeholerPattern3 in line:
print line
target.write(line.replace(placeholerPattern1, <variable>))
target.close()
當我檢查新文件中的值,然後這些都不會被替換。我可以看到源的價值也沒有改變,但內容已經改變,我在這裏做錯了什麼?