我如何用Python解析’Front Matter’

我如何用Python解析’Front Matter’,第1张

概述我似乎无法解释如何用 Python解析’Front Matter’.我有以下内容: ---name: Davidpassword: dwewwsadasemail: [email protected]: Website NamewebsitePrefix: websiteprefixwebsiteDomain: domain.comaction: create- 我似乎无法解释如何用 Python解析’Front Matter’.我有以下内容:

---name: DavIDpassword: DWewwsadasemail: [email protected]: Website namewebsitePrefix: websiteprefixwebsiteDomain: domain.comaction: create---

我正在使用以下代码:

Listing = os.Listdir(path)for infile in Listing:    stream = open(os.path.join(path,infile),'r')    docs = yaml.load_all(stream)    for doc in docs:        for k,v in doc.items():            print k,"->",v    print "\n",

由于第二组—我一直都会遇到错误

解决方法 我知道这是一个老问题,但我遇到了同样的问题,并使用了python-frontmatter.以下是向Front事件添加新变量的示例:

import frontmatterimport iofrom os.path import basename,splitextimport glob# Where are the files to modifypath = "en/*.markdown"# Loop through all filesfor fname in glob.glob(path):    with io.open(fname,'r') as f:        # Parse file's front matter        post = frontmatter.load(f)        if post.get('author') == None:            post['author'] = "alex"            # Save the modifIEd file            newfile = io.open(fname,'w',enCoding='utf8')            frontmatter.dump(post,newfile)            newfile.close()

资料来源:How to parse frontmatter with python

总结

以上是内存溢出为你收集整理的我如何用Python解析’Front Matter’全部内容,希望文章能够帮你解决我如何用Python解析’Front Matter’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193189.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存