File Format Issues¶
JSON Format Issues¶
Problem: Invalid JSON format.
Solutions:
# 1. Validate JSON before loading
import json
def validate_json_file(file_path):
try:
with open(file_path, 'r') as f:
json.load(f)
return True
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
return False
# 2. Fix common JSON issues
def fix_json_file(file_path):
with open(file_path, 'r') as f:
content = f.read()
# Fix common issues
content = content.replace("'", '"') # Replace single quotes
content = content.replace("True", "true") # Fix boolean values
content = content.replace("False", "false")
content = content.replace("None", "null")
with open(file_path, 'w') as f:
f.write(content)
# 3. Use proper JSON formatting
def format_json_file(file_path):
with open(file_path, 'r') as f:
data = json.load(f)
with open(file_path, 'w') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
TOML Format Issues¶
Problem: Invalid TOML format.
Solutions:
# 1. Validate TOML
import toml
def validate_toml_file(file_path):
try:
with open(file_path, 'r') as f:
toml.load(f)
return True
except toml.TomlDecodeError as e:
print(f"Invalid TOML: {e}")
return False
# 2. Fix common TOML issues
def fix_toml_file(file_path):
with open(file_path, 'r') as f:
content = f.read()
# Fix common issues
content = content.replace("True", "true")
content = content.replace("False", "false")
content = content.replace("None", "null")
with open(file_path, 'w') as f:
f.write(content)
YAML Format Issues¶
Problem: Invalid YAML format.
Solutions:
# 1. Validate YAML
import yaml
def validate_yaml_file(file_path):
try:
with open(file_path, 'r') as f:
yaml.safe_load(f)
return True
except yaml.YAMLError as e:
print(f"Invalid YAML: {e}")
return False
# 2. Fix common YAML issues
def fix_yaml_file(file_path):
with open(file_path, 'r') as f:
content = f.read()
# Fix common issues
content = content.replace("True", "true")
content = content.replace("False", "false")
content = content.replace("None", "null")
with open(file_path, 'w') as f:
f.write(content)