如何将文本文件写入具有精确缩进和格式的 yaml 文件 python
How to write text file into yaml file with exact identation and formatting in python
我有一个带有缩进的文本文件,我希望将该 txt 文件转换为具有精确构思的 YAML。
下面是我的文本文件 Source.txt
source:
dialect:sql
host:RATDegeggPRD
user:454the6yht
pass:4fghrthrthrt6h
auth:ldap
database: "rvvrrf"
queries: ["wrfrfrgvrv.sql"]
autoInfer: true
rows: -1
target:
database: "ddfcvdss"
schema: "dev"
queryTables: ["vrvrfre"]
mode: "write"
logfile: "rttcfdwee.log"
现在我想要这个文件,但它是 YAML 格式,具有准确的标识。
下面是我尝试做的
from pathlib import Path
import yaml
src_yaml = open("Source.txt", "r")
yaml_source = src_yaml.read()
in_file = Path('Source.txt')
out_file = in_file.with_suffix('.yaml')
yaml.dump(yaml_source, out_file)
现在,此代码正在生成一个 YAML 文件,其间包含所有“\n”。
在下面添加我的示例输出
"source:\n\tdialect:teradata\n\thost:RATDegeggPRD\n\tuser:454the6yht\n\tpass:4fghrthrthrt6h\n\
\tauth:ldap\n database: \"SCMAIN_V\"\n queries: [\"vrervvrec.sql\"\
]\n autoInfer: true\n rows: -1\n\ntarget:\n database: \"vrerevr\"\n schema: \"\
dev\"\n queryTables: [\"vrrrev\"]\n\nmode: \"write\"\nlogfile:\
\ \"fsefsfds.log\"\n"
我不想要所有这些 \n 或类似的东西。我想要准确的缩进
with open("source.txt", "r") as source, open("dest.yml", "wb") as dest:
dest.write(source.read())
这对我有用。
我有一个带有缩进的文本文件,我希望将该 txt 文件转换为具有精确构思的 YAML。
下面是我的文本文件 Source.txt
source:
dialect:sql
host:RATDegeggPRD
user:454the6yht
pass:4fghrthrthrt6h
auth:ldap
database: "rvvrrf"
queries: ["wrfrfrgvrv.sql"]
autoInfer: true
rows: -1
target:
database: "ddfcvdss"
schema: "dev"
queryTables: ["vrvrfre"]
mode: "write"
logfile: "rttcfdwee.log"
现在我想要这个文件,但它是 YAML 格式,具有准确的标识。
下面是我尝试做的
from pathlib import Path
import yaml
src_yaml = open("Source.txt", "r")
yaml_source = src_yaml.read()
in_file = Path('Source.txt')
out_file = in_file.with_suffix('.yaml')
yaml.dump(yaml_source, out_file)
现在,此代码正在生成一个 YAML 文件,其间包含所有“\n”。 在下面添加我的示例输出
"source:\n\tdialect:teradata\n\thost:RATDegeggPRD\n\tuser:454the6yht\n\tpass:4fghrthrthrt6h\n\
\tauth:ldap\n database: \"SCMAIN_V\"\n queries: [\"vrervvrec.sql\"\
]\n autoInfer: true\n rows: -1\n\ntarget:\n database: \"vrerevr\"\n schema: \"\
dev\"\n queryTables: [\"vrrrev\"]\n\nmode: \"write\"\nlogfile:\
\ \"fsefsfds.log\"\n"
我不想要所有这些 \n 或类似的东西。我想要准确的缩进
with open("source.txt", "r") as source, open("dest.yml", "wb") as dest:
dest.write(source.read())
这对我有用。