【发布时间】:2022-01-11 15:48:46
【问题描述】:
我正在尝试将一个 yml 文件拆分为两个新的 yml 文件。第一个文件只包含键,第二个文件只包含值。
这是我的代码:
# Import
from itertools import zip_longest
import yaml
import re
# Saves values to another yml file
with open("testFile.yml") as a_file:
for object in a_file:
stripped_object = object.rstrip()
found = False
file = open("ValuesfileNotTranslated.yml", "a")
if re.split(':|=', stripped_object, maxsplit=1)[-1].strip():
file.writelines(re.split(':|=', stripped_object, maxsplit=1)[-1].strip() + "\n" )
# Saves keys to another yml file
with open("testFile.yml") as a_file:
for object in a_file:
if object[:object.find(":")]:
file = open("Keysfile.yml", "a")
file.writelines(object[:object.find(":")] + ":" + "\n")
else:
file.writelines(object)
这是我要拆分的 yml 文件:
channels:
channel: Channel
headline: Channels
empty_msg: There are currently no channels.
add: Add new channel
reorder: Change channel order
actions:
show: View
edit: Edit
remove: Remove
当我尝试运行代码时,我得到了 2 个输出:
- 文件 1:仅包含密钥(此文件正确)
channels:
channel:
headline:
empty_msg:
add:
reorder:
actions:
show:
edit:
remove:
注意:共有 10 行。
- 文件 2:仅包含值(此文件不正确)
Channel
Channels
There are currently no channels.
Add new channel
Change channel order
View
Edit
Remove
注意:只有 8 行。 第二个文件也应该是 10 行。例如 file 1 中的 line 3 必须与 file 2 中的 line 3 匹配(例如 "headline" = "频道”)
当我们查看输入的 yml 文件时:
channels:
channel: Channel
例如,我们看到这 2 行。 “频道:”和“频道”被保存到密钥文件中,但唯一保存到值文件中的是“频道”。因此“通道:”之后的空格不会保存到值文件中。我认为存在问题,但我无法弄清楚我必须在代码中进行哪些更改才能获得正确的输出。
有人可以帮忙吗?
谢谢!
【问题讨论】:
-
当你使用
rstrip()时,你会删除行中最后一个非空白字符之后的所有空白,所以当你尝试在其上运行re.split()时,它只返回 1结果而不是两个。 -
@MattDMo 但是当我删除
rstrip()时,什么也没有发生。当我删除rstrip()和strip()时,会创建一个额外的空格。