当前位置:   article > 正文

用ChatGPT生成测试数据_chatpg 生产测试数据

chatpg 生产测试数据

大家好,欢迎来到 Crossin的编程教室 !

在之前的文章 用ChatGPT写一个数据采集程序 中,我们演示了如何用 ChatGPT 辅助编写代码。

除了直接让ChatGPT写代码,我们也可以让它生成一些开发中使用的测试数据。

比如在开发和测试时,常会用到 JSON 格式的数据:

4c1c1d57b5322d7ee0ed0dff58e90f6a.png

  1. [
  2. {
  3. "id": 1,
  4. "name": "John Smith",
  5. "age": 28,
  6. "gender": "male",
  7. "email": "john.smith@example.com",
  8. "address": "123 Main Street, City",
  9. "phone": "123-456-7890"
  10. },
  11. {
  12. "id": 2,
  13. "name": "Jane Doe",
  14. "age": 32,
  15. "gender": "female",
  16. "email": "jane.doe@example.com",
  17. "address": "456 Oak Avenue, Town",
  18. "phone": "987-654-3210"
  19. },
  20. {
  21. "id": 3,
  22. "name": "David Johnson",
  23. "age": 45,
  24. "gender": "male",
  25. "email": "david.johnson@example.com",
  26. "address": "789 Elm Street, Village",
  27. "phone": "555-123-4567"
  28. },
  29. {
  30. "id": 4,
  31. "name": "Emily Williams",
  32. "age": 21,
  33. "gender": "female",
  34. "email": "emily.williams@example.com",
  35. "address": "321 Pine Avenue, Town",
  36. "phone": "777-888-9999"
  37. }
  38. ]

如果对数据内容或结构不满意,可以让ChatGPT做进一步修改,比如增加一个存在重复数据的字段:

2e3c2a60be31eceb586bdb7919a2d500.png

  1. [
  2. {
  3. "id": 1,
  4. "name": "John Smith",
  5. "age": 28,
  6. "gender": "male",
  7. "email": "john.smith@example.com",
  8. "address": "123 Main Street, City",
  9. "phone": "123-456-7890",
  10. "occupation": "engineer"
  11. },
  12. {
  13. "id": 2,
  14. "name": "Jane Doe",
  15. "age": 32,
  16. "gender": "female",
  17. "email": "jane.doe@example.com",
  18. "address": "456 Oak Avenue, Town",
  19. "phone": "987-654-3210",
  20. "occupation": "doctor"
  21. },
  22. {
  23. "id": 3,
  24. "name": "David Johnson",
  25. "age": 45,
  26. "gender": "male",
  27. "email": "david.johnson@example.com",
  28. "address": "789 Elm Street, Village",
  29. "phone": "555-123-4567",
  30. "occupation": "teacher"
  31. },
  32. {
  33. "id": 4,
  34. "name": "Emily Williams",
  35. "age": 21,
  36. "gender": "female",
  37. "email": "emily.williams@example.com",
  38. "address": "321 Pine Avenue, Town",
  39. "phone": "777-888-9999",
  40. "occupation": "engineer"
  41. }
  42. ]

数据的格式也可以很方便地调整,比如从JSON换成XML:

51f14abc871d74289f96fadfcc9693f0.png

  1. <users>
  2. <user>
  3. <id>1</id>
  4. <name>John Smith</name>
  5. <age>28</age>
  6. <gender>male</gender>
  7. <email>john.smith@example.com</email>
  8. <address>123 Main Street, City</address>
  9. <phone>123-456-7890</phone>
  10. <occupation>engineer</occupation>
  11. </user>
  12. <user>
  13. <id>2</id>
  14. <name>Jane Doe</name>
  15. <age>32</age>
  16. <gender>female</gender>
  17. <email>jane.doe@example.com</email>
  18. <address>456 Oak Avenue, Town</address>
  19. <phone>987-654-3210</phone>
  20. <occupation>doctor</occupation>
  21. </user>
  22. <user>
  23. <id>3</id>
  24. <name>David Johnson</name>
  25. <age>45</age>
  26. <gender>male</gender>
  27. <email>david.johnson@example.com</email>
  28. <address>789 Elm Street, Village</address>
  29. <phone>555-123-4567</phone>
  30. <occupation>teacher</occupation>
  31. </user>
  32. <user>
  33. <id>4</id>
  34. <name>Emily Williams</name>
  35. <age>21</age>
  36. <gender>female</gender>
  37. <email>emily.williams@example.com</email>
  38. <address>321 Pine Avenue, Town</address>
  39. <phone>777-888-9999</phone>
  40. <occupation>engineer</occupation>
  41. </user>
  42. </users>

有同学要说了,可以用 faker 模块来生成哎!

是的,但那样毕竟也要自己动手写代码,还可能时不时要调整。现在有ChatGPT的辅助,可以秒出数据,岂不是更轻松?

当然你也可以将二者结合,比如这样:

5d24c5b6c00c209cd9293649ff84c8e2.png

  1. import random
  2. from faker import Faker
  3. import json
  4. faker = Faker()
  5. def generate_user_data(num_users):
  6. users = []
  7. for _ in range(num_users):
  8. user = {
  9. "id": faker.random_number(digits=4),
  10. "name": faker.name(),
  11. "age": random.randint(18, 60),
  12. "gender": random.choice(["male", "female"]),
  13. "email": faker.email(),
  14. "address": faker.address(),
  15. "phone": faker.phone_number()
  16. }
  17. users.append(user)
  18. return users
  19. num_users = 4
  20. user_data = generate_user_data(num_users)
  21. json_data = json.dumps(user_data, indent=4)
  22. print(json_data)

以往在开发中,如果需要类似的测试数据,手动编写是非常耗时和低效的。现在,类似的很多编程辅助工作都可交由ChatGPT来处理,从而让开发者把时间花在更重要的事情之上。


以上内容节选自Crossin的新书《码上行动:零基础学会PYTHON编程(CHATGPT版)》

本书力求做到浅显易懂,让完全没有编程经验的零基础“小白”也能学会Python。内容从环境搭建这种最基础的步骤开始讲起,逐渐深入到常见的实际应用当中。在讲解知识点的同时配有相应的代码示例,让读者可以边学边练加深理解。

全书涵盖Python环境搭建、基础语法、常见数据类型、实用模块、正则表达式、面向对象编程、多任务编程等知识点。另外还提供了爬虫、GUI、游戏三个实战项目。

书中还创新地使用 ChatGPT 作为编程学习的辅助,带领读者探索AI时代下学习编程的新模式。

f96815a4d140b2d2805018c5226df465.jpeg

感谢转发点赞的各位~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/365414
推荐阅读
相关标签
  

闽ICP备14008679号