怎樣將指定文字中的字串替換後生成新的文字檔案

2021-03-03 22:07:50 字數 2355 閱讀 9114

1樓:

import os

os.chdir('d:\\') # 跳到d盤

if not os.path.exists('pp.txt'): # 看一下這個bai

檔案du是否存zhi在

exit(-1) #,不存在就退出

lines = open('pp.txt').readlines() #開啟檔案,讀入每一行dao

fp = open('pp2.txt','w') #開啟你要寫

回得檔案pp2.txt

for s in lines:

fp.write( s.replace('love','hate').replace('yes','no')) # replace是替換,write是寫入答

fp.close() # 關閉檔案

python將指定文字中的字串替換後,生成新的文字檔案。

2樓:

import os

os.chdir('d:\\') # 跳到d盤

if not os.path.exists('pp.txt'): # 看一下這個檔案是否存在

exit(-1) #,不存在就退出

lines = open('pp.txt').readlines() #開啟檔案,讀入每一行

fp = open('pp2.txt','w') #開啟你要寫得檔案pp2.txt

for s in lines:

fp.write( s.replace('love','hate').replace('yes','no')) # replace是替換,write是寫入

fp.close() # 關閉檔案

3樓:曹曉山

python替換某個文

本中的字串,然後生成新的文字文件,**如下:

import os

os.chdir('d:\\') # 跳到d盤

if not os.path.exists('test1.txt'): # 看一下這個檔案是否存在

exit(-1) #不存在就退出

lines = open('test1.txt').readlines() #開啟檔案,讀入每一行

fp = open(''test2.txt','w') #開啟你要寫得檔案test2.txt

for s in lines:

# replace是替換,write是寫入

fp.write( s.replace('love','hate').replace('yes','no'))

fp.close() # 關閉檔案

4樓:匿名使用者

infile = open("d:/pp.txt", "r") #開啟檔案

outfile = open("d:/pp2.txt", "w") # 內容輸出

for line in infile: #按行讀檔案,可避免檔案過大,記憶體消耗

outfile.write(line.replace('love', 'hate').replace('yes','no'))

infile.close() #檔案關閉

outfile.close()

fortran中查詢某個字串,用另一字串替換,並生成新檔案

5樓:shine雪瑞

這個檔案的格式是:

abc\n

def\n

ghi\0

想要修改def 就可以把這段內容讀進一個字串陣列,第一個元素[0]是'a' , [4]元素是'd',修改[4][5][6]元素為多少,然後重新寫入檔案,用覆蓋的方法。

matlab怎麼用新的字串替換檔案中的字串,並寫在原字串位置?

6樓:魚尾摯愛白菜

用命令bai strrep(s1,s2,s3) ,在字串dus1裡所有的

zhis2被s3代替。

比如dao以下內例子:

容s1='this is a good boy';

strrep(s1,'good','great')returns 'this is a great boy';

strrep(s1,'bad','great') returns 'this is a good boy';

strrep(s1,'','great') returns 'this is a good boy';

在VB中,怎樣尋找字串中的第N個指定字元的位置

注意函式 findstrn option explicit private sub command1 click dim s as string s abs01b902h9dso0h2e70de210j0q dim x as integer 第1個 0 的位置 x findstrn s,0 1 pr...

Python的XML節點替換或字串替換問題

fpr open text.txt data fpr.read n 0for i in range len data if data i and data i 1 n 1 newdata data 0 i str n a str n fora k ibreak for i in range k 1,...

怎麼在notepad裡面,將字串替換成換行

1 首先開啟notepad 的主程式,需要在程式的頂欄選單欄開啟檢視的按鈕。2 接著就會顯示符號 顯示所有的字元。3 可以看到crlf就是回車換行符,箭頭就是製表符縮排,點就是空格。4 然後點選頂欄選單的搜尋按鈕,找到 查詢 選項。5 檢視那裡輸入 r n,同時需要把擴充套件開啟,這樣就可以正常的搜...