Categories
Python Answers

How to strip non printable characters from a string in Python?

Sometimes, we want to strip non printable characters from a string in Python.

In this article, we’ll look at how to strip non printable characters from a string in Python.

How to strip non printable characters from a string in Python?

To strip non printable characters from a string in Python, we can call the isprintable method on each character and use list comprehension.

For instance, we write

s = ''.join(c for c in my_string if c.isprintable())

to check if each character in my_string is printable with isprintable.

And we return an iterator with all the printable characters with

c for c in my_string if c.isprintable()

Then we call ''.join with the iterator to join the printable characters in my_string back to a string.

Conclusion

To strip non printable characters from a string in Python, we can call the isprintable method on each character and use list comprehension.

Categories
Python Answers

How to do interprocess communication in Python?

Sometimes, we want to do interprocess communication in Python.

In this article, we’ll look at how to do interprocess communication in Python.

How to do interprocess communication in Python?

To do interprocess communication in Python, we can use the multiprocessing library to send and receive commands.

For instance, in the server, we write

from multiprocessing.connection import Listener

address = ('localhost', 6000)
listener = Listener(address, authkey=b'secret password')
conn = listener.accept()
print('connection accepted from', listener.last_accepted)
while True:
    msg = conn.recv()
    if msg == 'close':
        conn.close()
        break
listener.close()

to call listener.accept to listen for for commands.

We then add an infinite while loop that ends when msg is 'close'.

We get msg from conn.recv which gets the received message.

In the client, we write

from multiprocessing.connection import Client

address = ('localhost', 6000)
conn = Client(address, authkey=b'secret password')
conn.send('close')
conn.close()

to create a Client object and call send to send a message to the server at the given address.

Conclusion

To do interprocess communication in Python, we can use the multiprocessing library to send and receive commands.

Categories
Python Answers

How to convert JSON string to a dictionary not list with Python?

Sometimes, we want to convert JSON string to a dictionary not list with Python.

In this article, we’ll look at how to convert JSON string to a dictionary not list with Python.

How to convert JSON string to a dictionary not list with Python?

To convert JSON string to a dictionary not list with Python, we call json.loads with the JSON string string and get the value by the index of the JSON string is an array string.

For instance, we write

json1_data = json.loads(json1_str)[0]

to call json.loads with the json1_str JSON array string and get the first item in the returned list with [0].

Conclusion

To convert JSON string to a dictionary not list with Python, we call json.loads with the JSON string string and get the value by the index of the JSON string is an array string.

Categories
Python Answers

How to print a string at a fixed width with Python?

Sometimes, we want to print a string at a fixed width with Python.

In this article, we’ll look at how to print a string at a fixed width with Python.

How to print a string at a fixed width with Python?

To print a string at a fixed width with Python, we can use the string format method.

For instance, we write

'{0: <5}'.format('s')

to call ‘{0: <5}’.formatwith‘s’` to print ‘s’ in a space 5 characters long and left aligned.

We use < to left align the string and we specify the length after that.

Conclusion

To print a string at a fixed width with Python, we can use the string format method.

Categories
Python Answers

How to check whether a file is empty or not with Python?

Sometimes, we want to check whether a file is empty or not with Python.

In this article, we’ll look at how to check whether a file is empty or not with Python.

How to check whether a file is empty or not with Python?

To check whether a file is empty or not with Python, we can use the os.stat method.

For instance, we write

import os

is_empty = os.stat("file.txt").st_size == 0

to get the size of file.txt with os.stat.

We get the size in bytes with st_size.

And if it’s 0, then the file is empty.

Conclusion

To check whether a file is empty or not with Python, we can use the os.stat method.