Bagaimana Anda mengganti teks tertentu dengan python?

Pada contoh berikut, kami akan mengganti string pyton dengan python di file data.txt, dan menulis hasilnya ke out.txt

Program Piton

#input file
fin = open("data.txt", "rt")
#output file to write the result to
fout = open("out.txt", "wt")
#for each line in the input file
for line in fin:
	#read replace the string and write to output file
	fout.write(line.replace('pyton', 'python'))
#close input and output files
fin.close()
fout.close()

Apa yang telah kita lakukan di sini?

  1. Buka data.txt dalam teks baca
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    1 mode dan dapatkan referensi ke
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    2
  2. Buka out.txt dalam mode tulis teks
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    4 dan dapatkan referensi ke
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    5
  3. Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    _6. untuk setiap baris di
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    _2 i. e. , data.txt,
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    9. mengganti string pyton dengan python dan
    Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
    2. menulis ke out.txt
  4. Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
    _4. menutup file yang direferensikan oleh
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    2,
    Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
    6. menutup file yang direferensikan oleh
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    5

Berkas Masukan

Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
_

Berkas keluaran

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
_

String pyton dalam file diganti dengan string python

Contoh 2. Ganti string di File yang sama

Pada contoh berikut, kita akan mengganti string python dengan python di data. txt, dan menimpa data. txt dengan teks yang diganti

Program Piton

#read input file
fin = open("data.txt", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('pyton', 'python')
#close the input file
fin.close()
#open the input file in write mode
fin = open("data.txt", "wt")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()

Apa yang telah kita lakukan di sini?

  1. Buka file data.txt dalam mode teks baca
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    1
  2. #read input file
    fin = open("data.txt", "rt")
    #read file contents to string
    data = fin.read()
    #replace all occurrences of the required string
    data = data.replace('pyton', 'python')
    #close the input file
    fin.close()
    #open the input file in write mode
    fin = open("data.txt", "wt")
    #overrite the input file with the resulting data
    fin.write(data)
    #close the file
    fin.close()
    
    2 membaca seluruh teks di data.txt ke variabel
    #read input file
    fin = open("data.txt", "rt")
    #read file contents to string
    data = fin.read()
    #replace all occurrences of the required string
    data = data.replace('pyton', 'python')
    #close the input file
    fin.close()
    #open the input file in write mode
    fin = open("data.txt", "wt")
    #overrite the input file with the resulting data
    fin.write(data)
    #close the file
    fin.close()
    
    4
  3. #read input file
    fin = open("data.txt", "rt")
    #read file contents to string
    data = fin.read()
    #replace all occurrences of the required string
    data = data.replace('pyton', 'python')
    #close the input file
    fin.close()
    #open the input file in write mode
    fin = open("data.txt", "wt")
    #overrite the input file with the resulting data
    fin.write(data)
    #close the file
    fin.close()
    
    5 menggantikan semua kemunculan pyton dengan python di seluruh teks
  4. Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
    _4 menutup file input data.txt
  5. Dalam tiga baris terakhir, kami membuka data.txt dalam mode tulis teks
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    4 dan menulis data ke data.txt dalam mode ganti. Akhirnya menutup file data.txt

Berkas Masukan

Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
_

File input yang sama setelah eksekusi program

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
_

Ringkasan

Dalam tutorial Contoh Python ini, kami belajar mengganti string dengan yang lain di file, dengan bantuan contoh yang terperinci

Bagaimana Anda mengganti nilai tertentu dalam string Python?

Sintaksis dari replace() .
Sintaksis. rangkaian. ganti (lama, baru, hitung)
Parameter
Nilai Pengembalian. Ini mengembalikan salinan string di mana semua kemunculan substring diganti dengan substring lain

Bagaimana Anda mengganti teks dengan Python?

Bagaimana caranya. .
old_text adalah parameter pertama yang dibutuhkan. ganti() menerima. Itu adalah karakter atau teks lama yang ingin Anda ganti. .
new_text adalah parameter kedua yang diperlukan. ganti() menerima. .
count adalah parameter ketiga opsional itu. ganti() menerima

Bagaimana cara mengganti string tertentu?

Metode Java string replace() akan mengganti karakter atau substring dengan karakter atau string lain. Sintaks untuk metode replace() adalah string_name. replace(old_string, new_string) dengan old_string menjadi substring yang ingin Anda ganti dan new_string menjadi substring yang akan menggantikannya.

Bagaimana Anda mengganti hanya satu kemunculan string dengan Python?

replace (old, new[, count]) -> string Kembalikan salinan string S dengan semua kemunculan substring lama diganti dengan yang baru. Jika hitungan argumen opsional diberikan, hanya kemunculan hitungan pertama yang diganti.