Home > Software design >  Replace text after string in python
Replace text after string in python

Time:11-30

I'm trying to replace string in a very big json File (30MB) so i try to automate this. Here is an example of what I have

"local_notifications": [
    {
      "is_enabled": false,
      "notification_type": "basic",
      "notification_title": "localised_strings.show_name_debug_lobbies",
      "notification_body": "localised_strings.show_desc_debug_lobbies",
      "schedule": {
        "schedule_type": "dynamic",
        "elapsed_time": [
          {
            "days": 0,
            "hours": 0,
            "minutes": 1
          }
        ]
      },
      "priority": 0,
      "priority_override": false,
      "id": "debug_notification"
    },

I would like to replace the notification title and body with his localised strings which are at the bottom at the file like this

"localised_strings": [
    {
      "text": "Debug lobbies",
      "id": "show_name_debug_lobbies"
    },
    {
      "text": "Debug lobbies 2",
      "id": "show_desc_debug_lobbies"
    }
 ]

I want to do this in python but i don't know how to do it can you help me ?

P.S. The only part of code I have is to decrypt the crypted file:

import argparse
from argparse import RawTextHelpFormatter

xor_key = [0x61, 0x23, 0x21, 0x73, 0x43, 0x30, 0x2c, 0x2e]

description = ('Decrypt or encrypt content_v1 of Fall Guys: Ultimate Knockout\n'
'content_v1 is usually found inside %UserProfile%\AppData\LocalLow\Mediatonic\FallGuys_client')

argument_parser = argparse.ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)

argument_parser.add_argument('input_file', help='content_v1 or a valid JSON file')
argument_parser.add_argument('output_file')

arguments = argument_parser.parse_args()

content = bytearray()
content_idx = 0

try:
  with open(arguments.input_file, 'rb') as input_file:
    while (byte := input_file.read(1)):
      content  = bytes([ord(byte) ^ xor_key[content_idx % (len(xor_key))]])
      content_idx  = 1
except (IOError, OSError) as exception:
  print('Error: could not read input file')
  exit()

try:
  with open(arguments.output_file, 'wb') as output_file:
    output_file.write(content)
except (IOError, OSError) as exception:
  print('Error: could not create output file')
  exit()

CodePudding user response:

Did you try to convert your Json into a dictionary? for instance using these resources: https://www.geeksforgeeks.org/convert-json-to-dictionary-in-python/

You convert to a dictionary, you edit the contents of the key you want, then save it back to Json if you need

  • Related