Python Forum
embedding data in a script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: embedding data in a script (/thread-33827.html)



embedding data in a script - Skaperen - May-31-2021

i have gotten AWS instances to run a Python script to do the instance initialization instead of bash. but this often needs to carry along chunks of data being used to create files on the instance. everything is in this one script and another script will be used to put it together and submit it. my first thought on bulk embedded data is triple quoted multiple lines of some kind of encoded data. what do others typically do in a case where a script will be creating some file (maybe binary, maybe text). can i do r""" to store raw binary such as compression output?


RE: embedding data in a script - Gribouillis - May-31-2021

You can use lexical strings concatenation to avoid triple quoted strings. Here is an example with data created by calling binascii.hexlify().decode() on raw binary data. I cut the hexlified string in chunks of 60 characters and write their Python representation on each line, with no comma between them, which concatenates them at lexical level.
data = binascii.unhexlify((
    '89504e470d0a1a0a0000000d4948445200000280000001e0080600000035'
    'd1dce40000003974455874536f667477617265004d6174706c6f746c6962'
    '2076657273696f6e332e342e312c2068747470733a2f2f6d6174706c6f74'
    '6c69622e6f72672fd9d40fa0000000097048597300000f6100000f6101a8'
    '3fa7690000a02849444154789cecdd777814e5dac7f1efee66d37b2f0442'
    ).encode())