Python3 Json转字典

1 双引号字符串

{
	"reviewerID": "A1XMHK9HN5MW2H", 
	"asin": "B00E2U4EM4", 
	"reviewerName": "Victoria J. Dennison", 
	"helpful": [1, 2], 
	"reviewText": "I rated the movie a one.  Although, I admire the effort of those involved in the making of this film; my advice to the public is not to waste your money.  I watched it, hoping that it would be halfway decent.  I was very disappointed.  The story line was almost non existent, and it was boring.  Just a man stumbling around in the dark with kids teasing him.  Watch that for over a hour...", 
	"overall": 1.0, 
	"summary": "The Last Light", 
	"unixReviewTime": 1388361600, 
	"reviewTime": "12 30, 2013"
}

import json
json.loads(json_str) # Json字符串转字典
json.dumps(dict) # 字典转Json字符串

import json

# 输入 - 文件
inPath = 'D:/projectData/EcSystem/Amazon/reviews.json'
inFile = open(inPath, 'r', encoding='UTF-8')


for line in inFile:
    try:
        # 去掉末尾换行符
        line = line.strip('\n')
        dict = json.loads(line)
        print(dict['reviewerID'])
    except:
        print(line)

inFile.close()

2 单引号字符串

{
	'asin': 'B00E2U4EM4', 
	'description': 'The scariest film since the Blair Witch! --Frances Burscough, Belfast TelegraghScared me sh*tless! Genuinely terrifying entertainment. --Mike Leeder, Impact MagazineShreds the nerves! --James Gracey, Behind The Couch', 
	'price': 30.5, 
	'imUrl': 'http://ecx.images-amazon.com/images/I/518QOYiMzNL._SY300_.jpg', 
	'related': 	{
					'also_bought': ['B00D49YFAA', 'B00GSA4FDO', 'B00IXD2MWK', 'B00J5G1MU2', 'B00J1Z3ZGG', 'B00DPUB56U', 'B00HVFA33G', 'B00D8MWW26', 'B00GOITWVU', 'B00FYV7W9W', 'B00EMAGIKU', 'B00DIR2PSW', 'B00FYV7WCY', 'B00J3CQGPE', 'B00HHYF56Q', 'B00ERH13BM', 'B00C6F622M', 'B00EZ50SGW', 'B00KHA99BY', 'B0055CP9DQ', 'B00H7BJ0C4', 'B00BEIYMAG', 'B00GWUU6MO', 'B00BTFK09G', 'B00BXRVQO8', 'B00DZP1C9K', 'B00GD7UO6E', 'B00DMEBXDE', 'B00IF8Q8C8', 'B00FRQ0HOQ', 'B00H4ZEFTQ', 'B00KACFMHO', 'B00I6JODGO', 'B00DENJICG', 'B00F3OLVHY', 'B00JF5GAZA', 'B00H47IXBK', 'B00F6Y3H1Y', 'B00G5GNYY2', 'B00J3M54AW', 'B00BLF9FLI', 'B00ERH1650', 'B00HV6AI6W', 'B00J81S92S', 'B00IF8Q8XC', 'B00DCLTE24', 'B00HRJVHNG', 'B00F3TD9GA', 'B00DBPBOZG', 'B00FUABHVQ', 'B00FPENI64', 'B0097C046W', 'B00DPUB5EW', 'B00I30MQA6', 'B00IYMSMTM', 'B00DNJ96MS', 'B00DNJ965A', 'B00E0KWBAI', 'B00HP4W1M4', 'B00GOYHSGK', 'B00C2R122Y', 'B00EZ50R04', 'B00FRQ0L2O', 'B00GAXASK8', 'B00BEIYG98', 'B006Z7Z3S6', 'B00FXOO1VC', 'B00AX5B0O2', 'B00I2YXN46', 'B00G7A5VZG', 'B00GS08UDU', 'B00HUAGYF8', 'B00E4V0JNY', 'B00GKP710G', 'B00DZP1C2W', 'B00EBBGKTE', 'B00HHYF570', 'B008RO6PJQ', 'B00GJNQ4B6', 'B00EE6ICIS', 'B00CYI4L6K', 'B00FEZXIZ0', 'B008WAM2E2', 'B00HGE90Z4', 'B00DY9FDR4', 'B00AN10NU8', 'B001KKU9CI', 'B003SWFLPG', 'B00HRCBGRA', 'B00B96TGQS', 'B00G0OM9TA', '0991097505', 'B00IJS2OR2', 'B00COQQ606', 'B006QWC2ZC', 'B00DZP1C8Q', 'B00FWSWR92', 'B00GB0OVY4', 'B00DOZNGTK', 'B00DW5IKN4'], 
					'bought_together': ['B00D49YFAA'], 
					'buy_after_viewing': ['B00D49YFAA', 'B00DPUB56U', 'B00DZP1C9K', '0783116926']
				}, 
	'salesRank': {'Movies & TV': 229335}, 
	'categories': [['#508510', 'Ordering', 'Amazon Marketplace', 'Pre-Orders'], ['Movies & TV', 'Movies']]
}

import ast
ast.literal_eval(json_str) # Json转字典

import ast

# 输入 - 文件
inPath = 'D:/projectData/EcSystem/Amazon/products.json'
inFile = open(inPath, 'r', encoding='UTF-8')

for line in inFile:
    try:
        # 去掉末尾换行符
        line = line.strip('\n')
        # string转dict
        outDict = ast.literal_eval(line)
        print(outDict['asin'])
    except:
        print("except", line)

inFile.close()

你可能感兴趣的:(Python3)