How to print a long string in multiple lines in Python3 - string

I recently came across python f-string. Let, there is a long line of string like
string = 'L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWScalA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1pQH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTtTvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMOK9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrxSdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qqfPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4dmjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJOUubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB'
I want to print this string like this:
L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWSc
alA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1p
QH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTt
TvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMO
K9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrx
SdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qq
fPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl
9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4d
mjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJO
UubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB
And I want to use F-strings. I have tried left alignment like
print(f"{string}:<{40}")
But it is not working.

This is what you use textwrap for:
import textwrap
string = 'L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWScalA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1pQH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTtTvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMOK9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrxSdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qqfPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4dmjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJOUubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB'
# print("{string}:<40")
print(textwrap.fill(string, 40))
L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWSc
alA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1p
QH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTt
TvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMO
K9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrx
SdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qq
fPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl
9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4d
mjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJO
UubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB

Related

How to split a string without touching some chars in python?

So I have this string:
'm(1,2),m(4,3)'
How can I split it to get list that contains only 2 elements:
['m(1,2)', 'm(4,3)']
I can't use str.split function because it will split the whole string and would give me something like this:
['m(1', '2)', 'm(4', '3)']
Can you help me?
You can try regex:
import re
regex = re.compile("m\([0-9],[0-9]\)")
s = "m(1,2),m(4,3)"
regex.findall(s)
should yield:
['m(1,2)', 'm(4,3)']

extract data between single quotes

trying to extract the data between single quotes
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(re.findall(r'()', a))
expecting the oputput : USA-APA HA-WBS-10.152.08.0/24
What is wrong with ? It is just a string ?
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(a)
Output:
% python3 test.py
USA-APA HA-WBS-10.152.08.0/24
You might want to look at this also regarding quotes and strings:
Single and Double Quotes | Python
I am not very familiar with python but with some quick searching around
I've found that this work
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
result = re.findall(r'(.*?)', a)
print("".join(result))
I'm pretty sure there are better ways of solving this but I'm not familiar with the language

How to python delete unwanted icon characters

Is there a way to get a string to get rid of icon characters automatically?
input: This is String 💋👌✅this is string✅✍️string✍️✔️
output wish: This is String this is stringstring
replace('💋👌✅', '') is not used because the icon character changes within each string without our prior knowledge of the content
Try this:
import re
def strip_emoji(text):
RE_EMOJI = re.compile(u'([\U00002600-\U000027BF])|([\U0001f300-\U0001f64F])|([\U0001f680-\U0001f6FF])')
return RE_EMOJI.sub(r'', text)
print(strip_emoji('This is String 💋👌✅this is string✅✍️string✍️✔️'))
Consider using the re module in Python to replace characters that you don't want. Something like:
import re
re.sub(r'[^(a-z|A-Z)]', '', my_string)

using a list from subprocess output

I use subprocess and python3 in the following script:
import subprocess
proc = subprocess.Popen("fail2ban-client -d".split(' '), stdout=subprocess.PIPE)
out, err = proc.communicate()
out.decode('ascii')
print(out)
The output is the following:
['set', 'syslogsocket', 'auto']
['set', 'loglevel', 'INFO']
['set', 'logtarget', '/var/log/fail2ban.log']
['set', 'dbfile', '/var/lib/fail2ban/fail2ban.sqlite3']
['set', 'dbpurgeage', 86400]
...
My issue is all this output is not a list. This is just a really big string with new line.
I have tried to convert each line to a list with this command:
eval(out.decode('ascii').split('\n')[0])
But I don't think this is the good way.
So my question is how can I convert a string (which looks like a list) to a list.
Though people generally are afraid of using eval, it is the way to go if you need to convert text literals into data. Instead of splitting up your (potentially long) string, you can simply read off the first line like so:
import io
f = io.StringIO(out.decode('ascii'))
first_list = eval(f.readline())
second_list = eval(f.readline())

How do I write a long string of words to a text file?

Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far.
Here is what I am trying to do
from nltk.tokenize import word_tokenize
my_list = word_tokenize(my_text)
my_list_string = ' '.join(my_list)
outfile = open("my_comp.txt", "a")
outfile.write(my_list_string )
outfile.close()
There is a library for that in Python named textwrap:
>>> import textwrap
>>> strs = "Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far. Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far. "
Works as below:
>>> print (textwrap.fill(strs, 20))
Is there a way to
write a long string
of words to a text
file in python3 so
that the string can
be written over
several lines? This
is what I have tried
so far. Is there a
way to write a long
string of words to a
text file in python3
so that the string
can be written over
several lines? This
is what I have tried
so far.
Or change the line length:
>>> print (textwrap.fill(strs, 40))
Is there a way to write a long string of
words to a text file in python3 so that
the string can be written over several
lines? This is what I have tried so far.
Is there a way to write a long string of
words to a text file in python3 so that
the string can be written over several
lines? This is what I have tried so far.
>>>
After seeing your code I'm not sure there is any other way then to insert a new line every nth token
But who knows how big n should be...
You should probably just leave it up to the program that is opening the file?

Resources