VDF Python Library

3.4 · active · verified Thu Apr 16

The `vdf` library is a pure Python module for serialization and deserialization of Valve's KeyValue (VDF) text and binary formats. It provides an interface similar to Python's built-in `json` module. The current version is 3.4, and it is actively maintained with an irregular release cadence, supporting KV1 format while KV2 and KV3 are not supported.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse and dump VDF data, including handling text and binary formats. It highlights the use of `vdf.VDFDict` for correctly managing duplicate keys and preserving order, which are common challenges with the VDF format.

import vdf
from collections import OrderedDict

vdf_text = '"Config" { "Key1" "Value1" "Key2" "Value2" "Key1" "AnotherValue" }'

# Deserialize VDF text to a Python dictionary (loses duplicate keys prior to Python 3.7)
data_dict = vdf.loads(vdf_text)
print(f"Standard dict (may lose duplicates): {data_dict}")

# Deserialize VDF text preserving order and handling duplicates with VDFDict
data_vdfdict = vdf.loads(vdf_text, mapper=vdf.VDFDict)
print(f"VDFDict (preserves order and duplicates): {data_vdfdict}")
print(f"Accessing a duplicated key in VDFDict: {data_vdfdict['Config']['Key1']}") # Returns a list

# Serialize a Python dictionary to VDF text
output_dict = {'Game': {'Name': 'MyGame', 'Version': '1.0'}}
vdf_output = vdf.dumps(output_dict, pretty=True)
print(f"\nSerialized VDF:\n{vdf_output}")

# Example of binary VDF (requires bytes input/output)
# For demonstration, we'll simulate binary data.
binary_vdf_bytes = b'\x00Config\x00\x00Key1\x00Value1\x00\x00Key2\x00Value2\x00\x08'
binary_data = vdf.binary_loads(binary_vdf_bytes)
print(f"\nDeserialized binary VDF: {binary_data}")

# Example of VBKV (ValueBinaryKeyValue) with header and CRC checking
# This often requires specific bytes patterns for actual Valve files.
# For a real scenario, vbkv_bytes would come from a file.
# Here, we'll just demonstrate the call.
# try:
#     vbkv_data = vdf.vbkv_loads(b'some_vbkv_bytes_with_header_and_crc')
#     print(f"Deserialized VBKV: {vbkv_data}")
# except Exception as e:
#     print(f"VBKV deserialization error (expected if bytes are not valid): {e}")

view raw JSON →