0

Insert newline after a letter in 1 line with Python

I have a plain-text file stored as .txt file called font.txt, the content of the file is one line CSS code. There are 675 different CSS classes. I want to separate them line by line so one line would contain one CSS class. Below is some part of the file: .ic-glass:before{content:"\i000"}.ic-align-right:before{content:"\i038"}. ic-square:before{content:"\i05b"}.ic-handshake:before{content:"\i1a6"} and etc... All 675 class inside the file is in one line, I want to separate them so I can read them easily.

20th Jun 2017, 7:03 AM
Dion Ricky
Dion Ricky - avatar
8 Respuestas
+ 2
I do not know CSS, but I'm assuming that you want a new line before each ".ic-". If there are no other dots in your file (except for the ones starting a new line), you can simply read the text, and: for line in mytext.split('.'): print('.'+line) (we need to add the dot back, since it is removed by split()). If there are other dots, you can use regular expressions: import re print(re.sub(r'\.ic-','\n.ic-',mytext))
20th Jun 2017, 7:19 AM
Bogdan Sass
Bogdan Sass - avatar
+ 1
I did check both codes in my interpreter (using the sample you provided), and they both worked: >>> mytext='.ic-glass:before{content:"\i000"}.ic-align-right:before{content:"\i038"}. ic-square:before{content:"\i05b"}.ic-handshake:before{content:"\i1a6"}' >>> import re >>> print(re.sub(r'\.ic-','\n.ic-',mytext)) .ic-glass:before{content:"\i000"} .ic-align-right:before{content:"\i038"}. ic-square:before{content:"\i05b"} .ic-handshake:before{content:"\i1a6"} (there is a missing newline here because there is an additional space between the dot and ic-square in your example). If you could provide your code and the results, maybe I can help you make the regex version work as well.
20th Jun 2017, 7:10 PM
Bogdan Sass
Bogdan Sass - avatar
+ 1
Oh, and one more thing - I know this is a programming forum, but I have to point out that there might be a simpler solution. Just use any text editor with regex search/replace support, and you can do a search for "\." and replace it with "\n\." In my case, I use Notepad++. (note for any nitpickers: yes, yes, the last backslash is not really necessary, but it makes things a bit more readable :) )
20th Jun 2017, 7:12 PM
Bogdan Sass
Bogdan Sass - avatar
+ 1
That's strange... it works for me: root@thermite:~/tmp# cat file.css .ic-glass:before{content:"\i000"}.ic-align-right:before{content:"\i038"}.ic-square:before{content:"\i05b"}.ic-handshake:before{content:"\i1a6".ic-glass:before{content:"\i000"}.ic-align-right:before{content:"\i038"}.ic-square:before{content:"\i05b"}.ic-handshake:before{content:"\i1a6" root@thermite:~/tmp# cat test.py #!/usr/bin/python3.2 import re myfile='file.css' with open(myfile,'r') as f: r=f.read() print(re.sub(r'\.ic-','\n.ic-',r)) root@thermite:~/tmp# ./test.py .ic-glass:before{content:"\i000"} .ic-align-right:before{content:"\i038"} .ic-square:before{content:"\i05b"} .ic-handshake:before{content:"\i1a6" .ic-glass:before{content:"\i000"} .ic-align-right:before{content:"\i038"} .ic-square:before{content:"\i05b"} .ic-handshake:before{content:"\i1a6" And (as you can see) I copy/pasted the code you gave here....
21st Jun 2017, 6:07 AM
Bogdan Sass
Bogdan Sass - avatar
+ 1
As for notepad++, here is a way to do the same thing directly from the editor: https://ibb.co/g5rfF5
21st Jun 2017, 6:11 AM
Bogdan Sass
Bogdan Sass - avatar
0
thanks for your answer. i've tried both, but the regex didn't add any line breaks, it's just printing the original file to python shell, but the split() works really amazing. Thank you very much, you are awesome!
20th Jun 2017, 2:26 PM
Dion Ricky
Dion Ricky - avatar
0
i have notepad++ but i didn't even know if it can do this job using search and replace. i've tried the regex so many times but still have the same result, my code: with open('font.txt','r') as file: r = file.read() import re print(re.sub(r'\.ic-','\n.ic-',r)) i'm pretty sure that i doesn't have any typo, but it still wont work.
20th Jun 2017, 9:14 PM
Dion Ricky
Dion Ricky - avatar
0
Thank you very much, it's very easy to do with notepad++, you've really helped me a lot, thanks
21st Jun 2017, 6:34 AM
Dion Ricky
Dion Ricky - avatar