我知道我错过了这里显而易见的东西,但我有以下PYTHON代码,我正在尝试 -
import json
def OpenJsonFileAndPullData (JsonFileName, JsonOutputFileName):
output_file=open(JsonOutputFileName, 'w')
result = []
with open(JsonFileName, 'r') as InputFile:
for line in InputFile:
Item=json.loads(line)
my_dict={}
print item
my_dict['Post Content']=item.get('content_text')
my_dict['Type of Post']=item.get('content_type')
print my_dict
result.append(my_dict)
json.dumps(result, output_file)
OpenJsonFileAndPullData ('MyInput.json', 'MyOutput.txt')
但是,运行时我收到此错误:
AttributeError: 'str' object has no attribute 'get'
答案 0 :(得分:0)
Python区分大小写。
Item = json.loads(line) # variable "Item"
my_dict['Post Content'] = item.get('content_text') # another variable "item"
顺便问一下,为什么不立刻将整个文件作为json加载?