Unbundle of a single file using python?

0

Hello, they asked me in an exam about doing "unbundle" of a file like this:

archivo1 Hola que tal?
archivo1 esto es una linea del archivo1
archivo2.py Sevilla tiene un color especial
archivo2.py if var in entry:
archivo3.txt Unbundle en varios archivos

I was asked to separate each line in a file, whose name is the first word, in this case archivo1 , archivo2.py or archivo3.txt , that is, pick the line read until there is 1 space and that is the name of the file, then after the space reads and writes that line in the file1, in this case, then next, it changes line and it is archivo2.py creates that file, and reads after the space and writes in the file the line.

I'm starting the top fp of DAW and the truth I can not catch up, remember that we have not given anything difficult, or complicated, I guess it is to do a open('archivo','r') , .write() , things like that, but I get stuck, thanks.

    
asked by Weketee 30.11.2018 в 20:16
source

1 answer

0

Without giving you the solution explicitly, because I understand that being a class work the solution you have to create it yourself, I give you some ideas of how to attack it, with the tools that you know how to handle at the moment, which I suppose are the following:

  • You know how to iterate through lists with a for loop.
  • You know some methods to operate on strings, like the .find() method to find a character within a string.
  • You know how to create lists and add items to them using .append()
  • You know the dictionaries and how to add elements to them or iterate over them.

With these tools, the pseudocode that you should be able to convert to Python would be the following:

1. Abrir fichero "bundle" y leerlo con 'fichero.readlines()'. 
   El resultado es una lista cuyos elementos son las líneas del archivo.
2. Crear un diccionario vacío. 
   La misión de ese diccionario será tener como claves los nombres de archivos,
   y como valores una lista con las líneas que deberían ir a ese fichero.
3. Iterar sobre la lista leída en el punto 1.
   3.1 Para cada elemento, aplicarle '.find(" ")' para localizar la aparición 
       del primer espacio. Guardar el resultado en una variable llamada espacio.
   3.2 Tomar elemento[:espacio] y eso será un nombre de archivo
   3.3 Tomar elemento[espacio+1:] y eso será una línea que ha de ir en ese archivo
   3.4 Si el nombre no estaba en el diccionario creado en 2
          hacer diccionario[nombre] = []
       y si no
          hacer diccionario[nombre].append(resto_linea)

Al salir del bucle el diccionario tendrá la información deseada, sus claves
serán los nombres de ficheros y sus valores las líneas de cada uno.

4. Iterar por el diccionario.
   4.1 Para cada elemento, usar la clave como nombre de archivo y crearlo
       con open(nombre, "w")
   4.2 Para cada elemento, el valor de esa entrada es una lista. Iterar por la lista
       4.2.1 Cada elemento de la lista escribirlo en el archivo
   4.3 Cerrar archivo

You can also avoid the construction of the dictionary if, as you process the lines in loop 3, you have in a variable which has been the last file name seen. When a new one appears, you close the previous file and open a new one. As long as it is the same you are going to pour lines in it. Although this approach avoids the dictionary, it is more difficult to implement correctly, because it has a more complex logic.

With these ideas you already have more than enough to start implementing. If during the implementation you find errors you can ask again (do not do it as an answer to this same question, but editing your original question or creating a new one) but do not forget to include the code of what you have done.

    
answered by 02.12.2018 в 13:32