Skip to main content
2 de 2
se añadieron 599 caracteres en el cuerpo
FJSevilla
  • 62.1k
  • 7
  • 43
  • 68

Para guardar el string con codificación no ASCII puedes usar el módulo io:

import io
import re

s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No misshaps no typos. No bugs. But I want the code too look good too. That's the problem!"

with io.open('filename.txt', 'w', encoding='utf-8') as myfile:
    myfile.write(u'\n&_\n'.join('"'+line.strip()+'"' for line in re.findall(r'.{1,10}(?:\s+|$)', s)))

Observación: He cambiado el número de caracteres en la expresión regular para obtener una salida que se pueda mostrar aqui. Cambiala adecuadamente.

Salida:

introducir la descripción de la imagen aquí

Edición:

Si lo que quieres es partir la cadena cada n caracteres sin importar nada más usa slicing:

import io

n = 8 #Numero caracteres por linea
s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No misshaps no typos. No bugs. But I want the code too look good too. That's the problem!"
with io.open('filename.txt', 'w', encoding='utf-8') as myfile:
    myfile.write(u'\n&_\n'.join('"'+s[i:i+n]+'"' for i in range(0, len(s), n)))
FJSevilla
  • 62.1k
  • 7
  • 43
  • 68