write code convert data.json file into data.csv file using python
时间: 2024-02-15 12:02:48 浏览: 70
Sure, here's a code snippet that converts a JSON file named "data.json" into a CSV file named "data.csv" using Python:
```python
import csv
import json
# Open the JSON file
with open('data.json') as f:
data = json.load(f)
# Open the CSV file and write headers
with open('data.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
# Write data to CSV file
for row in data:
writer.writerow(row)
```
This code uses Python's built-in `csv` and `json` modules to read the JSON data from the file, write headers to the CSV file, and write each row of data to the CSV file. Note that the first row of the JSON data is assumed to contain the headers for the CSV file.
阅读全文