我如何用Python解析’Front Matter’
发布时间:2020-12-20 11:37:36 所属栏目:Python 来源:网络整理
导读:我似乎无法解释如何用 Python解析’Front Matter’.我有以下内容: ---name: Davidpassword: dwewwsadasemail: david@domain.comwebsiteName: Website NamewebsitePrefix: websiteprefixwebsiteDomain: domain.comaction: create--- 我正在使用以下代码: li
我似乎无法解释如何用
Python解析’Front Matter’.我有以下内容:
--- name: David password: dwewwsadas email: david@domain.com websiteName: Website Name websitePrefix: websiteprefix websiteDomain: domain.com action: 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 frontmatter import io from os.path import basename,splitext import glob # Where are the files to modify path = "en/*.markdown" # Loop through all files for 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |