create json file dump the value

Please post any questions about developing your plugin here. Please use the search function before posting!
varunkishore
Junior Member
Posts: 11
Joined: Wed Mar 28, 2018 6:47 am

create json file dump the value

Postby varunkishore » Sat Apr 28, 2018 1:40 pm

here i can create the json file ,in that i can print the player killed victim but
it prints like this only in file.json ({"Name": "Chet", "Killer name": "Finn", "weapon type": "bizon"}) LAST ONE KILLED DATA ONLY

but in console it prints like this and same as game log also

"Troy" was killed by "Cory" by using this weapon "m249"
{'Name': 'Troy', 'Killer name': 'Cory', 'weapon type': 'm249'}
"Dave" was killed by "Vladimir" by using this weapon "awp"
{'Name': 'Dave', 'Killer name': 'Vladimir', 'weapon type': 'awp'}
"Adrian" was killed by "Gabe" by using this weapon "p90"
{'Name': 'Adrian', 'Killer name': 'Gabe', 'weapon type': 'p90'}
"Vladimir" was killed by "Dave" by using this weapon "inferno"
{'Name': 'Vladimir', 'Killer name': 'Dave', 'weapon type': 'inferno'}

Syntax: Select all

import os

@Event('player_death')
def on_player_death(game_event):
userid = game_event['userid']
attacker = game_event['attacker']
victim = Player.from_userid(userid)
killer = Player.from_userid(attacker)
weapon = game_event['weapon']
SayText2 ('"{}" was killed by "{}" by using this weapon "{}" and steam id is'.format(victim.name, killer.name, weapon)).send()
print ('"{}" was killed by "{}" by using this weapon "{}"'.format(victim.name, killer.name, weapon))
op = open('file.json', 'w')
dict = {'Name': victim.name, 'Killer name': killer.name,'weapon type': weapon}
print (dict)
op.write(json.dumps(dict))
op.close()


i need to print same like console print data (like every death) in json file itself.....with time
help me complete this code
Last edited by satoon101 on Sat Apr 28, 2018 2:47 pm, edited 1 time in total.
Reason: Python syntax highlighting
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: create json file dump the value

Postby satoon101 » Sat Apr 28, 2018 2:46 pm

When you open a file in 'write' mode, you are overwriting the entire file. If you want to 'append' to the file, open it in 'append' mode:

Syntax: Select all

op = open('file.json', 'a')


Also, for future reference, when posting Python code on the forums, use [python][/python] instead of [code][/code]

Another tidbit for you: you can use the "with" statement to automatically open/close the file:

Syntax: Select all

with open('file.json', 'a') as op:
dict = {'Name': victim.name, 'Killer name': killer.name,'weapon type': weapon}
op.write(json.dumps(dict))
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 11 guests