I need to make a Python script to edit a specific line from the config. Assume:

{ "name": "Insane Augewehr", "type": "Premium Workshop Augewehr Skin", "description": "Painted on his gun, the absolute madman!\n\n<color=#64c819>Workshop Contributors:\nDebski</color>\n\nWhen equipped this applies the <color=#64c819>Insane</color> appearance to the Augewehr in-game.", "name_color": "64c819", "itemdefid": 757, "marketable": false, "scraps": 0, "item_id": 1362, "item_skin": 170, "item_effect": 0, "vehicle_id": 0 }, 

From this part of the config, I need to suppose we replace item_id with 1200 and item_skin with 175. In the end, it should look like this:

  { "name": "Insane Augewehr", "type": "Premium Workshop Augewehr Skin", "description": "Painted on his gun, the absolute madman!\n\n<color=#64c819>Workshop Contributors:\nDebski</color>\n\nWhen equipped this applies the <color=#64c819>Insane</color> appearance to the Augewehr in-game.", "name_color": "64c819", "itemdefid": 757, "marketable": false, "scraps": 0, "item_id": 1200, "item_skin": 175, "item_effect": 0, "vehicle_id": 0 }, 

Tell me, how can you implement a similar replacement through python?

    1 answer 1

    JSON can be processed through the standard json module. For example, you can load data from a config file and parse it with the json module into dict :

     import json s = """{ "name": "Insane Augewehr", "type": "Premium Workshop Augewehr Skin", "description": "Painted on his gun, the absolute madman!", "name_color": "64c819", "itemdefid": 757, "marketable": false, "scraps": 0, "item_id": 1362, "item_skin": 170, "item_effect": 0, "vehicle_id": 0 }""" d = json.loads(s) print(d) 

    After loading, the variable d is a dictionary (dict) in which you can change the values ​​by standard methods. After the changes, the variable d can be saved to a file.

    Another version of the Python script:

     import re import os filename = './test.json' # Открываем 2 файла, один читаем, в другой суффиксом .new пишем: with open(filename, 'r') as fp1, open(filename + '.new', 'a') as fp2: for line in fp1: if line.find('item_id') > 0: # Если встроке находится нужный параметр (подстрока) line = re.sub(r'[0-9]+', '1200', line, 1) # То меняем у этого параметра значение с помощью регулярного выражения elif line.find('item_skin') > 0: line = re.sub(r'[0-9]+', '175', line, 1) fp2.write(line) # Заменяем старый файл ./test.json содержимым из ./test.json.new os.rename(filename + '.new', filename) 
    • Yes, well, but I need to edit the lines already in the finished json file. Suppose we have a settings.json file in which we have the line "name": "Insane Weapon", - we need to change it to any other name. Can you give an example of such actions? - Slavatar 4:39 pm
    • @Slavatar - "change in the finished file" - these are the three steps - open the file, change something in it and save (overwrite) the ALL file. What do you want? Change bytes at a certain position in a gigabyte file? I do not know what kind of file system allows. Conventional filesystems can only append (append) to the end of a file or overwrite it completely as far as I know. - microcoder
    • @Slavatar What example do you need? How to open a file and read it line by line or what example? - microcoder
    • Hah, well, something like this. It is necessary to say so, in secret from the user to change certain lines in the config. Config does not weigh more than 50, 30 or even 20mb. - Slavatar 2:44 pm
    • @Slavatar Added an option. In the comments explanation. - microcoder