Python 使用元数据创建文件名
如果可以用一些atributes创建md文件并在python上保存为.md格式? 所以我有心房肌:Python 使用元数据创建文件名,python,python-3.x,markdown,Python,Python 3.x,Markdown,如果可以用一些atributes创建md文件并在python上保存为.md格式? 所以我有心房肌: year="2018" month="12" year="2018" month="12" path_folder="C:\\Users\" MD_output = path_folder + year +'-'+ month +'_metadata.md' md_file - what should be here? md_file.write(MD_output) 我需要保存它,例如,保
year="2018"
month="12"
year="2018"
month="12"
path_folder="C:\\Users\"
MD_output = path_folder + year +'-'+ month +'_metadata.md'
md_file - what should be here?
md_file.write(MD_output)
我需要保存它,例如,保存到2018_12_metadata.md文件,其中包含所需的属性。下面是md文件的外观
{
"year": "2018",
"month": "12"
}
因此,我有一些代码,但我可以找到如何将其保存为md格式,并使用所需的atributes:
year="2018"
month="12"
year="2018"
month="12"
path_folder="C:\\Users\"
MD_output = path_folder + year +'-'+ month +'_metadata.md'
md_file - what should be here?
md_file.write(MD_output)
非常感谢您的帮助。编一本字典
metadata = {
"year": "2018",
"month": "12"
}
设置文件路径
path_folder="C:\\Users\\"
md_file = "{}\\{year}_{month}_metadata.md".format(path_folder, *metadata)
打开一个文件并写入它
with open(md_file, 'w') as f:
f.write("foobar")
或者,如果您只想将字典写入文件,请将其作为JSON加载
import json
with open(md_file, 'w') as f:
json.dump(metadata, f)
亲爱的,这是我找到的解决方案:
result = {'year': '2018',
'month': '12'}
file_name="Check"
with open(os.path.join(path_folder, '{}.md'.format(file_name)), mode='w') as md_file:
json.dump(result, md_file, indent=2)
谢谢大家的帮助