Advertisement
melvinSanGerman

Empaquetando líneas por carácteres.

Sep 18th, 2023 (edited)
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | Source Code | 0 0
  1. En este código de Python organizaremos grupos de paquetes organizados de líneas de acuerdo a su cantidad de caracteres.
  2.  
  3.  
  4. #!/usr/bin/python3
  5. texto = """0123456789
  6. 1234567890
  7. 2345678901
  8. 3456789012
  9. 4567890123xxxxxxx
  10. 5678901234
  11. 6789012345x
  12. 7890123456
  13. 8901234567890123456789012345678901234567890123456789
  14. 9012345678
  15. 8765432109
  16. 7654321098x
  17. 6543210987
  18. 5432109876"""
  19.  
  20. paquete = []
  21. maxlenght = 44
  22. contenido = ""
  23. conteo = 0
  24.  
  25. line = texto.split("\n")
  26. i = -1
  27. while i < len(line)-1:
  28.     i+=1
  29.     conteo += len(line[i]) +1
  30.     if len(line[i]) +1 > maxlenght:
  31.         i += 1
  32.     if conteo <= maxlenght:
  33.        contenido += line[i] + "\n"
  34.        if i == len(line)-1:
  35.             paquete += [contenido]
  36.     elif conteo >= maxlenght:
  37.         conteo = 0
  38.         paquete += [contenido]
  39.         contenido = ""
  40.         i -=1
  41.            
  42. for i in paquete:
  43.    print(i)
  44.  
  45.    
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement