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.