简体   繁体   中英

extracting single value from json file to text file

i have json file like this

{"lomx": "3 lokan", "idx": "kojemany94AaABAg", "moraku": "loerlaosjdis hejuyasdb", "keyword": "mekanobhaemausjek14232", "subhen": "koshdeksad"}
{"lomx": "3 lokan", "idx": "kojemany94AaABAg", "moraku": "londasjhdj", "keyword": "mekanobhaemausjek223232323", "subhen": "uahdioasdohoasd"}
{"lomx": "4 lokan", "idx": "jhalskdjhakljdisoaj", "moraku": "vajdlajd", "keyword": "mekanobhaemausjek3242312345", "subhen": "jshdajshdajhsdjhaksdjhsjdkahsdjkah"}
{"lomx": "4 lokan", "idx": "jahdjheuhasndashduasd", "moraku": "morkajsdhu", "keyword": "mekanobhaemausjek273232323232", "subhen": "lokamajuaksdksdasdaahsjdhkajhsdjkahs"}

i need to extract all the single values of "keyword" and add it to text file line by line

result should be something like this

mekanobhaemausjek14232
mekanobhaemausjek223232323
mekanobhaemausjek3242312345
mekanobhaemausjek273232323232

this is what i've tried

import json
data_file = 'c:\json_file.json'
json_data = open(data_file)
parsed_json = json.loads(json_data)

for mydata in parsed_json['keyword']
    print (mydata)
    f = open('jessers.txt','a')
    f.write("%s\n" % mydata)
    f.close()

It seems that your program has a little problem, below is my test. There is no error now. Maybe you can modify it to suit your needs.

import json
data_file = 'c:\json_file.json'
json_data = open(data_file)
json_text = json_data.readlines()

for data_index in json_text:
    parsed_json = json.loads(data_index)
    print (parsed_json['keyword'])

OUtput:

mekanobhaemausjek14232
mekanobhaemausjek223232323
mekanobhaemausjek3242312345
mekanobhaemausjek273232323232

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM