看板 Python 關於我們 聯絡資訊
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