Re: [問題] 如何同時讀寫一個檔案?

看板Python作者 (阿祐)時間9年前 (2014/09/14 00:13), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
searchline = ' \n' inputfile = open('a.php','r') lines = inputfile.readlines() outputfile = open('b.php','w') for line in lines: if line==searchline : outputfile.write('Different random text\n') else : outputfile.write(line) ※ 引述《mymiss312 (mymiss)》之銘言: : 請問一下我在嘗試自動加資料到同一個檔案的實作 : <?php : $arr = array( : array("A","B"), : array("B","C"),) : ?> : array 大約有500多行 : 我想要讀取到空白行的時候塞值進去 : 最後的目的是要自動加資料到同一個檔案 : 不過我試著分開寫 也是得到同樣的結果(會有缺漏) : 想請問有沒有什麼解決方法呢?? : 以下是我的程式碼: : searchline = '\n' : inputfile = open('a.php','r') : lines = inputfile.readlines() # f being the file handle : i = lines.index(searchline) # Make sure searchline is actually in the file : lines.insert(i+1, 'Different random text') : outputfile = open('b.php','w') : for line in lines: : outputfile.write(line) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.70.186.199 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1410624836.A.F32.html
文章代碼(AID): #1K56r4yo (Python)
文章代碼(AID): #1K56r4yo (Python)