首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将yml文件转换为文本文件?

如何将yml文件转换为文本文件?
EN

Stack Overflow用户
提问于 2022-01-06 11:04:00
回答 1查看 88关注 0票数 0

我正在尝试将一个yml文件转换为一个文本文件。这是我的密码:

代码语言:javascript
复制
#Import Modules
from ruamel.yaml import YAML
import yaml

#Read file
with open("/Users/AndreB/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/Tests/ymlProject/MOOCnlVersion1.yml", 'r') as stream:
    try:
        loaded = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(exc)

#File Url
file_read = "/Users/AndreB/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/Tests/ymlProject/MOOCnlVersion1.yml"

#Loop through file
for key, value in yaml.safe_load(open(file_read)).items():
    #Write file
    with open("YamlTextFileVersion1.txt", 'w') as stream:
        try:
            yaml.dump(loaded, stream, default_flow_style=False)
        except yaml.YAMLError as exc:
            print(exc)

yaml文件由5000多行组成。这是文件的一部分:

代码语言:javascript
复制
course:
    courses:
      date:
        self_paced: Self-paced
        beginning: "Beginning %{start_date}"
        since: "Since %{start_date}"
        range: "%{start_date} - %{end_date}"
        coming_soon: Coming soon
      index:
        headline: "Our courses: smart. social. <span class='primary-color'>free.</span>"
        subheadline: "Join one of our free and open courses and be part of the social learning experience!"
        current: Current courses
        upcoming: Upcoming courses
        archive: Self-paced courses
        archive_text: Note that you can still enroll to finished courses and access them for self-learning (without the chance to earn a Record of Achievement).
        in_preparation: Courses in preparation (unpublished)
        partner_courses: Partner courses
        partner_courses_text: You might also want to have a look at these courses provided by our partners.
        hidden: Hidden Courses

但是,当我试图运行代码时,我会得到以下错误消息:

代码语言:javascript
复制
Exception has occurred: ScannerError
while scanning a simple key
  in "/Users/AndreB/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/Tests/ymlProject/MOOCnlVersion1.yml", line 455, column 9
could not find expected ':'
  in "/Users/AndreB/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/Tests/ymlProject/MOOCnlVersion1.yml", line 456, column 9
  File "/Users/AndreB/Library/Mobile Documents/com~apple~CloudDocs/Programming/Python/Tests/ymlProject/TestConvertToTxt.py", line 20, in <module>
    for key, value in yaml.safe_load(open(file_read)).items():

有人能帮我吗?

@blurryroot文件中还有不应该翻译的行(用“不翻译”表示)

代码语言:javascript
复制
 news: # Do not translate
        title: News Statistics # Do not translate
        globaal: # Do not translate
          title: Globaal News # Do not translate
        course: # Do not translate
          title: Course Announcements # Do not translate
        news_title_header: News title # Do not translate
        course_title_header: Course title # Do not translate
        total_header: Total # Do not translate
        success_header: Success # Do not translate
        error_header: Error # Do not translate
        disabled_header: Disabled # Do not translate
        read_header: Read # Do not translate
        newest_header: Newest # Do not translate
        oldest_header: Oldest # Do not translate
        state_header: Status # Do not translate
        progress_header: Progress # Do not translate
        readstate_progress_header: ReadState Progress # Do not translate
        state_text: # Do not translate
          text_sending: sending # Do not translate
EN

回答 1

Stack Overflow用户

发布于 2022-01-06 12:52:32

您可以使用黑名单作为下一个迭代开发步骤。

代码语言:javascript
复制
event:
  news:
    field_to_ignore: true

在处理事件数据以将其交给翻译时(例如,,如您在评论中提到的那样),您将检查该字段是否包含在黑名单yaml中。如果是这样,只需跳过。否则就翻译。

您可以根据所选语言将翻译后的数据集存储起来,以供以后查找。

长远来看,可能值得重新考虑数据结构,并使用已经存在的体系结构和库,如@scotty3785 3785在评论中指出的i18n。正确地将公共数据从依赖于语言的数据中分离。

这个图书馆这篇文章可能会给您带来一些关于如何构造数据的灵感。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70606134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档