我在檔案“code.s”中有以下代碼:
movi $1,10
beginning: jeq $1, $0,done
addi $1, $1,-1
j beginning
done: halt
我想在檔案中的每個“:”字符之后直接插入一個換行符 '\n'。我將如何在 Python 中執行此操作?
uj5u.com熱心網友回復:
根據檔案的大小,您可以嘗試將其作為字串讀入記憶體。
然后使用該yourstring.replace(target, replacement)函式,將每個替換":"為":\n".
將此資料寫回您的檔案:
with open("code.s", "w") as the_file:
# This will delete the contents of the file, make a backup
the_file.write(yourstring)
uj5u.com熱心網友回復:
import os
os.chdir(r'C:\Users\LAVANYA\OneDrive\Documents\am3')
file=open('am2.txt','r')
doc=file.readlines()
a=[]
for i in doc:
a.append(i.replace(":",":\n"))
file=open('am2.txt','w')
file.writelines(a)
file.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325091.html
下一篇:如何使用C中的檔案進行此操作?
