替换文件中的一个词以添加另一个文件内容

huangapple go评论63阅读模式
英文:

Replacing a word in a file to add another file content

问题

      "file": "NAME,TYPE,ACCESS_TOKEN\n dev765,datatype,Token765\n dev879,datatype,Token879",
英文:

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.

答案1

得分: 1

尝试这种方法。

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 ")

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

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

测试代码:

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
  }
}
英文:

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
  }
}

huangapple
  • 本文由 发表于 2023年3月12日 18:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定