入门教程、案例源码、学习资料、读者群
请访问: python666.cn
大家好,欢迎来到 Crossin的编程教室 !
在之前的文章 用ChatGPT写一个数据采集程序 中,我们演示了如何用 ChatGPT 辅助编写代码。
除了直接让ChatGPT写代码,我们也可以让它生成一些开发中使用的测试数据。
比如在开发和测试时,常会用到 JSON 格式的数据:
[ { "id": 1, "name": "John Smith", "age": 28, "gender": "male", "email": "john.smith@example.com", "address": "123 Main Street, City", "phone": "123-456-7890" }, { "id": 2, "name": "Jane Doe", "age": 32, "gender": "female", "email": "jane.doe@example.com", "address": "456 Oak Avenue, Town", "phone": "987-654-3210" }, { "id": 3, "name": "David Johnson", "age": 45, "gender": "male", "email": "david.johnson@example.com", "address": "789 Elm Street, Village", "phone": "555-123-4567" }, { "id": 4, "name": "Emily Williams", "age": 21, "gender": "female", "email": "emily.williams@example.com", "address": "321 Pine Avenue, Town", "phone": "777-888-9999" } ]
如果对数据内容或结构不满意,可以让ChatGPT做进一步修改,比如增加一个存在重复数据的字段:
[ { "id": 1, "name": "John Smith", "age": 28, "gender": "male", "email": "john.smith@example.com", "address": "123 Main Street, City", "phone": "123-456-7890", "occupation": "engineer" }, { "id": 2, "name": "Jane Doe", "age": 32, "gender": "female", "email": "jane.doe@example.com", "address": "456 Oak Avenue, Town", "phone": "987-654-3210", "occupation": "doctor" }, { "id": 3, "name": "David Johnson", "age": 45, "gender": "male", "email": "david.johnson@example.com", "address": "789 Elm Street, Village", "phone": "555-123-4567", "occupation": "teacher" }, { "id": 4, "name": "Emily Williams", "age": 21, "gender": "female", "email": "emily.williams@example.com", "address": "321 Pine Avenue, Town", "phone": "777-888-9999", "occupation": "engineer" } ]
数据的格式也可以很方便地调整,比如从JSON换成XML:
<users> <user> <id>1</id> <name>John Smith</name> <age>28</age> <gender>male</gender> <email>john.smith@example.com</email> <address>123 Main Street, City</address> <phone>123-456-7890</phone> <occupation>engineer</occupation> </user> <user> <id>2</id> <name>Jane Doe</name> <age>32</age> <gender>female</gender> <email>jane.doe@example.com</email> <address>456 Oak Avenue, Town</address> <phone>987-654-3210</phone> <occupation>doctor</occupation> </user> <user> <id>3</id> <name>David Johnson</name> <age>45</age> <gender>male</gender> <email>david.johnson@example.com</email> <address>789 Elm Street, Village</address> <phone>555-123-4567</phone> <occupation>teacher</occupation> </user> <user> <id>4</id> <name>Emily Williams</name> <age>21</age> <gender>female</gender> <email>emily.williams@example.com</email> <address>321 Pine Avenue, Town</address> <phone>777-888-9999</phone> <occupation>engineer</occupation> </user> </users>
有同学要说了,可以用 faker 模块来生成哎!
是的,但那样毕竟也要自己动手写代码,还可能时不时要调整。现在有ChatGPT的辅助,可以秒出数据,岂不是更轻松?
当然你也可以将二者结合,比如这样:
import random from faker import Faker import json faker = Faker() def generate_user_data(num_users): users = [] for _ in range(num_users): user = { "id": faker.random_number(digits=4), "name": faker.name(), "age": random.randint(18, 60), "gender": random.choice(["male", "female"]), "email": faker.email(), "address": faker.address(), "phone": faker.phone_number() } users.append(user) return users num_users = 4 user_data = generate_user_data(num_users) json_data = json.dumps(user_data, indent=4) print(json_data)
以往在开发中,如果需要类似的测试数据,手动编写是非常耗时和低效的。现在,类似的很多编程辅助工作都可交由ChatGPT来处理,从而让开发者把时间花在更重要的事情之上。