我正在尝试将一个yml文件转换为一个文本文件。这是我的密码:
#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多行组成。这是文件的一部分:
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但是,当我试图运行代码时,我会得到以下错误消息:
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文件中还有不应该翻译的行(用“不翻译”表示)
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发布于 2022-01-06 12:52:32
https://stackoverflow.com/questions/70606134
复制相似问题