Posts

Convert String to Base64 in Python

Image
Code to convert string (from utf-8) to base64 encoding and base64 back to utf-8: import base64 si = input('\nenter text:\n') print('\nyou entered\n' + si) b64 = base64.b64encode(bytes(si, 'utf-8')) print('\nbase64 is\n' + str(b64)) b64_bs = bytearray(base64.b64encode(bytes(si, 'utf-8'))) print('\nbase64 byte stream is\n' + str(list(b64_bs))) so = base64.b64decode(b64).decode('utf-8', 'ignore') print('\nstring is\n' + so) This example encodes a string to Base64 and decodes it back in Python using the base64 module. Base64 represents arbitrary bytes as plain text, which is useful when binary data must travel through text only channels such as JSON or email. Remember that Base64 is an encoding, not encryption: it provides no secrecy and grows the data by about a third. Encode for transport, not for protection.

Convert String to Binary in Python

Image
Code to convert string to binary and binary to string: def string2bits(s=''):     return [bin(ord(x))[2:].zfill(8) for x in s] def bits2string(b=None):     return ''.join([chr(int(x, 2)) for x in b]) si = input('\nenter text:\n') b = string2bits(si) so = bits2string(b) print('\nyou entered\n' + si) print('\nbinary is\n' + str(b)) print('\nstring is\n' + so) This example converts text to its binary representation and back in Python by mapping each character to its 8 bit code with ord and bin, then reversing the process with int and chr. It is a useful exercise for understanding character encoding and how text is stored as bytes underneath. For real binary handling, Python's bytes type and struct module are the practical tools.

TypeError: Can't convert 'bytes' object to str implicitly

Image
Error: Traceback (most recent call last):   File "b64-str.py", line 10, in <module>     print('\nstring is\n' + so) TypeError: Can't convert 'bytes' object to str implicitly Problem Code: so = base64.b64decode(b64) print('\nstring is\n' + so) Solution: so = base64.b64decode(b64).decode('utf-8', 'ignore') print('\nstring is\n' + so) This Python 3 error happens when you join bytes and str with the plus operator. Python 3 keeps bytes and text strictly separate, so they cannot be concatenated directly. Calling .decode() on the bytes, or .encode() on the string, converts one to the other so the types match. This is one of the most common surprises when moving code from Python 2 to Python 3.

AttributeError: module 'json' has no attribute 'dump'

Image
Problem: Today I was trying to create a json file from Python and then I got this error: "AttributeError: module 'json' has no attribute 'dump'" and I found my solution here: http://stackoverflow.com/a/13612382/4064166 Solution: So the problem was I named my file as json.py that was causing problems with json module so renaming it solved the issue. Code: import json with open("dump.json", "w") as outfile:     json.dump({'number':6650, 'strings':'lorem ipsum', 'x':'x', 'y':'y'}, outfile, sort_keys = True, indent=4, ensure_ascii=False) Screenshot:

Make file read-only in Python

Image
If you want to make file read-only: import os from stat import S_IREAD os.chmod('path/to/file.txt', S_IREAD) If you want to make file writeable: import os from stat import S_IWRITE os.chmod('path/to/file.txt', S_IWRITE) Ref. 1: http://stackoverflow.com/a/28492823/4064166 Ref. 2: http://techarttiki.blogspot.in/2008/08/read-only-windows-files-with-python.html This snippet toggles a file between read-only and writable in Python using os.chmod with stat flags. Setting S_IREAD makes it read-only; S_IWRITE makes it writable again. It is handy for protecting generated output or configuration from accidental edits. On Unix like systems the effect also depends on ownership and the existing permissions.

Create PDF from Python

Image
I was googling for a python library that I can use to write strings to a PDF file but mostly I found out programs to copy data from one PDF and write it to another PDF, another programs and solutions I found were for editing existing PDF, it was not what I was looking for. I wanted to create a PDF from scratch. After hours of searching finally I found my solution when I arrived on this page by David Fischer and it just uses one module called reportlab . Code is share via this gist on my GitHub.

TypeError: write() argument must be str, not bytes

Image
Recently I encountered this error: "TypeError: write() argument must be str, not bytes" when trying to write a PDF file from anaconda 3. The solution was to write stream in binary. Error: J:\>python pdf.py Traceback (most recent call last):   File "pdf.py", line 31, in <module>     fd.write(buf.getvalue()) TypeError: write() argument must be str, not bytes Problem code: # Write the PDF to a file with open('test.pdf', 'w' ) as fd:     fd.write(buf.getvalue()) Solution: # Write the PDF to a file with open('test.pdf', 'wb' ) as fd:     fd.write(buf.getvalue()) Ref.: http://stackoverflow.com/a/5513856 This Python error happens when binary data is written to a file opened in text mode, which is common when producing a PDF or other binary output. Opening the file in binary mode, for example with the wb flag, lets you write bytes directly and resolves it. Writing text to a file opened in binary mo...