Advertisement
Tkap1

NoRegex

Jan 29th, 2019
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. with open("textos.txt") as source: # Abrir el archivo base
  2.     fullText = source.read() # Guardar el texto en una variable
  3.     fullText = fullText.split("\n") # Separar todas las lineas
  4.    
  5.     title = fullText[0] # Guardar la primera linea (asumiendo que la primera linea es el primer titulo)
  6.     currentText = "" # Variable para guardar el texto que va despues de cada titulo
  7.     for i in range(1, len(fullText)): # Empezamos el loop en la segunda linea
  8.         line = fullText[i] # La linea de esta iteracion
  9.         if line.startswith("."): # Si la linea empieza por "."
  10.             with open(title + ".txt", "w") as output: # Creamos un archivo con el mismo nombre que el titulo
  11.                 output.write(title + "\n" + currentText) # Escribimos el "titulo" + "contenido" en el archivo
  12.             title = line # Hemos llegado al siguiente titulo, asi que el nuevo titulo es la linea de esta iteracion
  13.         else:
  14.             currentText += line + "\n" # Si la linea no empieza por ".", es parte del contenido del ultimo titulo
  15.            
  16.     # Al final del loop creamos otro archivo porque el loop no pillaria el ultimo titulo + contenido
  17.     with open(title + ".txt", "w") as output:
  18.         output.write(title + "\n" + currentText)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement