我有一個JSON文件,我想轉換爲CSV文件。我如何用Python做到這一點?如何將JSON轉換爲CSV?
我想:
import json
import csv
f = open('data.json')
data = json.load(f)
f.close()
f = open('data.csv')
csv_file = csv.writer(f)
for item in data:
f.writerow(item)
f.close()
然而,它沒有工作。我使用Django和我收到的錯誤是:
file' object has no attribute 'writerow'
所以,後來我試過如下:
import json
import csv
f = open('data.json')
data = json.load(f)
f.close()
f = open('data.csv')
csv_file = csv.writer(f)
for item in data:
csv_file.writerow(item)
f.close()
然後我得到的錯誤:
sequence expected
樣品JSON文件:
[
{
"pk": 22,
"model": "auth.permission",
"fields": {
"codename": "add_logentry",
"name": "Can add log entry",
"content_type": 8
}
},
{
"pk": 23,
"model": "auth.permission",
"fields": {
"codename": "change_logentry",
"name": "Can change log entry",
"content_type": 8
}
},
{
"pk": 24,
"model": "auth.permission",
"fields": {
"codename": "delete_logentry",
"name": "Can delete log entry",
"content_type": 8
}
},
{
"pk": 4,
"model": "auth.permission",
"fields": {
"codename": "add_group",
"name": "Can add group",
"content_type": 2
}
},
{
"pk": 10,
"model": "auth.permission",
"fields": {
"codename": "add_message",
"name": "Can add message",
"content_type": 4
}
}
]
我們需要更多的細節。請從json文件發佈示例 – 2009-12-09 04:08:26
您可以發佈json文件嗎?直到你做到,我們無法真正幫助你。 – 2009-12-09 05:58:01
我有我的json文件示例。 謝謝 – 2009-12-09 06:24:00