JSON vers CSV
Convertissez des tableaux JSON au format CSV pour les feuilles de calcul.
Questions Fréquentes
Implémentation du Code
import csv
import json
import io
data = [
{"name": "Alice", "age": 30, "city": "New York"},
{"name": "Bob", "age": 25, "city": "London"},
]
# Using csv.DictWriter
with open("output.csv", "w", newline="", encoding="utf-8") as f:
if data:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
# Using pandas (recommended for complex data)
import pandas as pd
df = pd.DataFrame(data)
df.to_csv("output.csv", index=False)
# From JSON string
json_str = '[{"x": 1, "y": 2}, {"x": 3, "y": 4}]'
df = pd.read_json(json_str)
csv_output = df.to_csv(index=False)
print(csv_output)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.