Skip to content

Instantly share code, notes, and snippets.

@timendum
Last active January 29, 2026 08:10
Show Gist options
  • Select an option

  • Save timendum/7c9041de8652523cd0391ed738e0fea5 to your computer and use it in GitHub Desktop.

Select an option

Save timendum/7c9041de8652523cd0391ed738e0fea5 to your computer and use it in GitHub Desktop.
How to edit Python VCR fixtures cassette
import gzip
import json
import yaml
from yaml import Dumper, Loader
FILENAME = "./test.yml"
with open(FILENAME) as fo:
tyaml = fo.read()
oyaml = yaml.load(tyaml, Loader=Loader)
# check for compression library in Content-Encoding
tjson = gzip.decompress(oyaml['interactions'][0]['response']['body']['string']).decode('utf-8')
# optional json parse
data = json.loads(tjson)
# Do your edit here
# same steps, backwards
tjson = json.dumps(data)
oyaml['interactions'][0]['response']['body']['string'] = gzip.compress(tjson.encode('utf-8'))
with open(".".join(FILENAME.split('.')[:-1]) + '_new.yml', 'w') as fo:
yaml.dump(oyaml, fo, Dumper=Dumper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment