MWE:
yt_link = "https://www.youtube.com/watch?v=8cQSCIWHwRQ"
process = subprocess.Popen(["you-get", "-i", "{}".format(yt_link)], shell=False, stdout=subprocess.PIPE)
process.wait()
output = str(process.communicate()) # c.f. ADDENDUM #1
list_output = output.split("#")
for i in list_output:
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
result = ansi_escape.sub('', i)
result = re.sub('\033\\[([0-9]+)(;[0-9]+)*m', '', result)
ansi_escape3 = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]', flags=re.IGNORECASE)
text = ansi_escape3.sub('', result)
if "mp4" in text:
print(text)
CURRENT OUTPUT:
download-with: \x1b[4myou-get --itag=278 [URL]\x1b[0m\n\n - itag: \x1b[7m394\x1b[0m\n container: mp4\n quality: 192x144 (144p)\n size: 12.7 MiB (13285482 bytes)\n
download-with: \x1b[4myou-get --itag=394 [URL]\x1b[0m\n\n - itag: \x1b[7m160\x1b[0m\n container: mp4\n quality: 192x144 (144p)\n size: 12.1 MiB (12663186 bytes)\n
QUESTION:
I'm trying to get rid of the ANSI characters and have tried various regex's however to no avail. How can I escape the regex characters, and dump the output into a list of dictionaries?
RESEARCH:
Looked up and read various sites:
- How can I remove the ANSI escape sequences from a string in python
- Python Code Examples for strip ansi
- How to remove the ANSI escape sequences from a string in Python?
IDEAL OUTPUT:
Ideally the print out should look like this,
download-with: you-get --itag=278 [URL] - itag: 278 container: mp4 quality: 192x144 (144p) size: 12.7 MiB (13285482 bytes)
download-with: you-get --itag=394 [URL] - itag: 394 container: mp4 quality: 192x144 (144p) size: 12.1 MiB (12663186 bytes)
However I'd like to dump it into a list of dictionaries:
[{
"download-with": "you-get --itag=278 [URL]",
"- itag": "278",
"container": "mp4",
"quality": "192x144 (144p)",
"size": "12.7 MiB (13285482 bytes)"
},
{
"download-with": "you-get --itag=394 [URL]",
"- itag": "394",
"container": "mp4",
"quality": "192x144 (144p)",
"size": "12.1 MiB (12663186 bytes)"
}]
* ADDENDUM #1:
As requested by @Cubed:
print out of output
(b'site: YouTube\ntitle: 53 Thieves - after hours\nstreams: # Available quality and codecs\n [ DASH ] ____________________________________\n - itag: \x1b[7m271\x1b[0m\n container: webm\n quality: 1600x1200 (1080p)\n size: 245.5 MiB (257452213 bytes)\n # download-with: \x1b[4myou-get --itag=271 [URL]\x1b[0m\n\n - itag: \x1b[7m400\x1b[0m\n container: mp4\n quality: 1600x1200 (1080p)\n size: 225.9 MiB (236852538 bytes)\n # download-with: \x1b[4myou-get --itag=400 [URL]\x1b[0m\n\n - itag: \x1b[7m137\x1b[0m\n container: mp4\n quality: 1440x1080 (1080p)\n size: 179.2 MiB (187938743 bytes)\n # download-with: \x1b[4myou-get --itag=137 [URL]\x1b[0m\n\n - itag: \x1b[7m248\x1b[0m\n container: webm\n quality: 1440x1080 (1080p)\n size: 142.4 MiB (149281731 bytes)\n # download-with: \x1b[4myou-get --itag=248 [URL]\x1b[0m\n\n - itag: \x1b[7m399\x1b[0m\n container: mp4\n quality: 1440x1080 (1080p)\n size: 132.9 MiB (139365006 bytes)\n # download-with: \x1b[4myou-get --itag=399 [URL]\x1b[0m\n\n - itag: \x1b[7m398\x1b[0m\n container: mp4\n quality: 960x720 (720p)\n size: 73.5 MiB (77038720 bytes)\n # download-with: \x1b[4myou-get --itag=398 [URL]\x1b[0m\n\n - itag: \x1b[7m247\x1b[0m\n container: webm\n quality: 960x720 (720p)\n size: 67.7 MiB (71014369 bytes)\n # download-with: \x1b[4myou-get --itag=247 [URL]\x1b[0m\n\n - itag: \x1b[7m136\x1b[0m\n container: mp4\n quality: 960x720 (720p)\n size: 51.3 MiB (53815143 bytes)\n # download-with: \x1b[4myou-get --itag=136 [URL]\x1b[0m\n\n - itag: \x1b[7m397\x1b[0m\n container: mp4\n quality: 640x480 (480p)\n size: 43.2 MiB (45295169 bytes)\n # download-with: \x1b[4myou-get --itag=397 [URL]\x1b[0m\n\n - itag: \x1b[7m244\x1b[0m\n container: webm\n quality: 640x480 (480p)\n size: 39.3 MiB (41227455 bytes)\n # download-with: \x1b[4myou-get --itag=244 [URL]\x1b[0m\n\n - itag: \x1b[7m135\x1b[0m\n container: mp4\n quality: 640x480 (480p)\n size: 30.6 MiB (32083018 bytes)\n # download-with: \x1b[4myou-get --itag=135 [URL]\x1b[0m\n\n - itag: \x1b[7m396\x1b[0m\n container: mp4\n quality: 480x360 (360p)\n size: 30.4 MiB (31899162 bytes)\n # download-with: \x1b[4myou-get --itag=396 [URL]\x1b[0m\n\n - itag: \x1b[7m243\x1b[0m\n container: webm\n quality: 480x360 (360p)\n size: 26.0 MiB (27215879 bytes)\n # download-with: \x1b[4myou-get --itag=243 [URL]\x1b[0m\n\n - itag: \x1b[7m134\x1b[0m\n container: mp4\n quality: 480x360 (360p)\n size: 22.6 MiB (23719355 bytes)\n # download-with: \x1b[4myou-get --itag=134 [URL]\x1b[0m\n\n - itag: \x1b[7m242\x1b[0m\n container: webm\n quality: 320x240 (240p)\n size: 18.4 MiB (19244079 bytes)\n # download-with: \x1b[4myou-get --itag=242 [URL]\x1b[0m\n\n - itag: \x1b[7m395\x1b[0m\n container: mp4\n quality: 320x240 (240p)\n size: 17.5 MiB (18399398 bytes)\n # download-with: \x1b[4myou-get --itag=395 [URL]\x1b[0m\n\n - itag: \x1b[7m133\x1b[0m\n container: mp4\n quality: 320x240 (240p)\n size: 15.8 MiB (16552880 bytes)\n # download-with: \x1b[4myou-get --itag=133 [URL]\x1b[0m\n\n - itag: \x1b[7m278\x1b[0m\n container: webm\n quality: 192x144 (144p)\n size: 13.5 MiB (14112534 bytes)\n # download-with: \x1b[4myou-get --itag=278 [URL]\x1b[0m\n\n - itag: \x1b[7m394\x1b[0m\n container: mp4\n quality: 192x144 (144p)\n size: 12.7 MiB (13285482 bytes)\n # download-with: \x1b[4myou-get --itag=394 [URL]\x1b[0m\n\n - itag: \x1b[7m160\x1b[0m\n container: mp4\n quality: 192x144 (144p)\n size: 12.1 MiB (12663186 bytes)\n # download-with: \x1b[4myou-get --itag=160 [URL]\x1b[0m\n\n [ DEFAULT ] _________________________________\n - itag: \x1b[7m18\x1b[0m\n container: mp4\n quality: medium\n size: 35.5 MiB (37201730 bytes)\n # download-with: \x1b[4myou-get --itag=18 [URL]\x1b[0m\n\n', None)