Pelican> 正文

Pelican 模板开发中使用Faker生成模拟数据

2023-11-05T22:21:33+08:00

  Pelican 模板开发中使用Faker生成模拟数据以便测试。 Faker官方文档:https://faker.readthedocs.io/en/master/index.html

1、安装

pip install Faker
pip install loguru

2、代码

放到项目文件夹下执行即可。

from faker import Faker
from loguru import logger
import os

fake = Faker()

fake = Faker("zh_CN")

def gen_data(num):
    for i in range(num):
        category = fake.province()
        for i in range(num):
            title = str(fake.paragraphs(nb=1)[0]).replace('.',"")[:20]
            tags= ','.join([fake.city_name() for x in range(6)])
            content = ''.join(fake.paragraphs(nb=50))
            description=title
            Postimage='Code_2023-07-08-40-37-3.jpg'
            datetime = fake.date_time(tzinfo=None, end_datetime=None)

            md_str=f'''title: {title}
Date: {datetime}
Category:{category}
Tags: {tags}
description:{description}
images: []
Postimage:{Postimage}

{content}
            '''
            try:
                os.mkdir(f'content/{category}')
            except Exception as e:
                pass
            with open(f'content/{category}/{title}.md', 'w') as f:
                f.write(md_str)


if __name__ == "__main__":
    gen_data(20)
分享到:

Ranvane的日常记录

关于我们 客服中心 广告服务 法律声明