how to split a very long string in python

1

Hi, I want to split a very long string, it will look like this:

"numerodecaracter1" 
&_ 
"numerodecaracter2"
 .. etc..

Try this

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!"

print '\n'.join(line.strip() for line in re.findall(r'.{1,80}(?:\s+|$)', s))

But I'm left as follows:

de 1 a 80 caracteres salto de linea
de 1 a 80 caracteres salto de linea

How could I make it look like the way I said:

"numerodecaracter1" 
&_ 
"numerodecaracter2"
&_ 
.. etc..

So my chain will splite but as the way it indicates. And as last I wrote in a file the result instead of a print but I get the error:

TypeError: 'encoding' is an invalid keyword argument for this function

Code:

s = "lalalalala" 

#print '\n'.join(line.strip() for line in re.findall(r'.{1,80}(?:\s+|$)', s))

with open('filename.txt', mode='wt', encoding='utf-8') as myfile:
        myfile.write('\n'.join(line.strip() for line in re.findall(r'.{1,80}(?:\s+|$)', s)))

I removed the enconding and it resolved the writing but when I use a long string and with symbols I break the whole chain so it is important that I can use the encoding.

Capture how I want it:

    
asked by Sergio Ramos 03.06.2017 в 03:01
source

1 answer

1

To save the string with non-ASCII coding you can use the io module:

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)))

Remark: I have changed the number of characters in the regular expression to get an output that can be displayed here. Change it appropriately.

Exit:

Edit:

If you want to split the string every n characters without importing anything else, use 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)))
    
answered by 03.06.2017 в 04:16