“como substituir uma palavra no arquivo de texto usando python” Respostas de código

Python Encontre e substitua a string no arquivo

# Read in the file
with open('file.txt', 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace('ram', 'abcd')

# Write the file out again
with open('file.txt', 'w') as file:
  file.write(filedata)
Frantic Fish

como substituir uma palavra no arquivo de texto usando python

# How to replace a word in a text file

filename = "sample1.txt"
# SAMPLE1.TXT
# Hello World!
# I am a human.

with open(filename, 'r+') as f:
    text = f.read()
    text = re.sub('human', 'cat', text)
    f.seek(0)
    f.write(text)
    f.truncate()

# SAMPLE1.TXT
# Hello World!
# I am a cat.
Alert Alligator

Respostas semelhantes a “como substituir uma palavra no arquivo de texto usando python”

Perguntas semelhantes a “como substituir uma palavra no arquivo de texto usando python”

Procure respostas de código populares por idioma

Procurar outros idiomas de código