2016-02-10 37 views
1

這是一個有效的JSON對象嗎?使用Python的有效JSON

{"Age": "2", "Name": "Rice, Master. Eugene", "Parch": "1", "Pclass": "3", "Ticket": "382652", "PassengerId": "17", "SibSp": "4", "Embarked": "Q", "Fare": "29.125", "Survived": "0", "Cabin": "", "Sex": "male"} 

我需要EOF嗎?

我已經使用以下內容來創建該文件:

import json 
import sys 
fieldnames=["PassengerId","Survived","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"] 
csvfile=open('t1.csv','r') 
jsonfile = open('file1.json', 'w') 
reader = csv.DictReader(csvfile, fieldnames) 
for row in reader: 
#  if reader.line_num ==1: 
       #continue # Skip the first line 
     json.dump(row, jsonfile) 
     jsonfile.write('\n') 
print("Total No of Lines Wriiten : "+ str(reader.line_num)) 
+0

是的,那就是JSON。您可以通過在瀏覽器中打開開發工具(在Chrome中按Ctrl + Shift + j)並在控制檯中鍵入'JSON.parse('...')'來測試它。 –

回答

0

簡單測試經由json.loads()

import json 

j = json.loads('{"Age": "2", "Sex": "male"}') 
print j 

或可替代地通過使用json.load()直接從已保存的文件加載它測試:

import json 

with open('file1.json', 'r') as f: 
    j = json.load(f) 
    print j 

...似乎是有效的。