我有这样的json文件
{"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"}
我需要提取“关键字”的所有单个值,并逐行将其添加到文本文件中
结果应该是这样的
mekanobhaemausjek14232
mekanobhaemausjek223232323
mekanobhaemausjek3242312345
mekanobhaemausjek273232323232
这就是我尝试过的
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()
看来你的程序有一点问题,下面是我的测试。现在没有错误。也许您可以根据自己的需要进行修改。
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'])
输出:
mekanobhaemausjek14232
mekanobhaemausjek223232323
mekanobhaemausjek3242312345
mekanobhaemausjek273232323232