1

Input file (csv).

NAME,TYPE,ACCESS_TOKEN
dev765,datatype,Token765
dev879,datatype,Token879

Append file:

{
  "file": "string",
  "mapping": {
    "columns": [
      {
        "type": "NAME"
      },
      {
        "type": "TYPE"
      },
      {
        "type": "ACCESS_TOKEN"
      }
    ],
    "delimiter": ",",
    "header": true,
    "update": true
  }
}

The goal is to replace the "string" in the 'Append file' with the content of the 'Input file', with line-break. So the resultant 'Append file' file should look like this:

{
  "file": "NAME,TYPE,ACCESS_TOKEN\n dev765,datatype,Token765\n dev879,datatype,Token879",
  "mapping": {
    "columns": [
      {
        "type": "NAME"
      },
      {
        "type": "TYPE"
      },
      {
        "type": "ACCESS_TOKEN"
      }
    ],
    "delimiter": ",",
    "header": true,
    "update": true
  }
}

What I actually need is the first line of the 'Append.txt' file as:

{
  "file": "_the_content_of_the_input_file_with_\n_break",
  REST OF THE FILE AS IT IS
  ....
  ....
}

I can append the entire file at the beginning or at the end if its plain text. Or replace a word with another. But just don't know how to do with the above format and how to replace a word with an entire file.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Fakir
  • 139
  • 11
  • It looks like the "append" file is JSON. So read it as JSON into a Python dictionary, replace the value of the "file" key with the file contents of the input file, and write it back as as JSON file. – mkrieger1 Mar 12 '23 at 10:26
  • 1
    Thanks for your reply and tips. Yes, it is a JSON file. I am not that proficient in coding. Use it infrequently if needed. I will try your suggestion and come back if I have trouble achieving it. – Fakir Mar 12 '23 at 10:37

1 Answers1

1

Try this approach.

with (open("input.csv", "r") as infile,
      open("append_file.json", "r") as append_file,
      open("output_file.json", "w") as outfile):

    infile_content = infile.read().strip()
    infile_content = infile_content.replace("\n", "\\n ")
    
    # You can also use the join() approach
    # infile_content = "\n ".join(infile.readlines())

    append_file_content = json.load(append_file)
    append_file_content["file"] = infile_content

    json.dump(append_file_content, outfile, indent=2)

Test Code:

import json

input_file_content = """NAME,TYPE,ACCESS_TOKEN
dev765,datatype,Token765
dev879,datatype,Token879
"""

input_file_content = "\n ".join(input_file_content.splitlines())

append_file_content = {
  "file": "string",
  "mapping": {
    "columns": [
      {
        "type": "NAME"
      },
      {
        "type": "TYPE"
      },
      {
        "type": "ACCESS_TOKEN"
      }
    ],
    "delimiter": ",",
    "header": True,
    "update": True
  }
}

append_file_content["file"] = input_file_content
print(json.dumps(append_file_content, indent=2))

{
  "file": "NAME,TYPE,ACCESS_TOKEN\n dev765,datatype,Token765\n dev879,datatype,Token879",
  "mapping": {
    "columns": [
      {
        "type": "NAME"
      },
      {
        "type": "TYPE"
      },
      {
        "type": "ACCESS_TOKEN"
      }
    ],
    "delimiter": ",",
    "header": true,
    "update": true
  }
}
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
  • 1
    Perfect! Thank you so much! I have also just tested with the output and it could send the test JSON values with the test API to the indented system platform and register the values. – Fakir Mar 12 '23 at 11:48