“Cómo editar una línea específica en el archivo de texto en Python” Código de respuesta

Cómo editar una línea específica en el archivo de texto en 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

Edición de una línea específica en el archivo de texto en 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

Respuestas similares a “Cómo editar una línea específica en el archivo de texto en Python”

Preguntas similares a “Cómo editar una línea específica en el archivo de texto en Python”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código