“Editando linha específica no arquivo de texto em python” Respostas de código

Como editar uma linha específica no arquivo de texto em Python

# with is like your try .. finally block in this case
with open('stats.txt', 'r') as file:
    # read a list of lines into data
    data = file.readlines()

print data
print "Your name: " + data[0]

# now change the 2nd line, note that you have to add a newline
data[1] = 'Mage\n'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Annoying Albatross

Editando linha específica no arquivo de texto em python

# with is like your try .. finally block in this case
with open('stats.txt', 'r') as file:
    # read a list of lines into data
    data = file.readlines()

print data
print "Your name: " + data[0]

# now change the 2nd line, note that you have to add a newline
data[1] = 'Mage\n'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Gentle Goshawk

Respostas semelhantes a “Editando linha específica no arquivo de texto em python”

Perguntas semelhantes a “Editando linha específica no arquivo de texto em python”

Procure respostas de código populares por idioma

Procurar outros idiomas de código