簡體   English   中英

使用Python的有效JSON

[英]Valid JSON using 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))

通過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

...似乎是有效的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM