帮我写一个随机生成数据的代码可定义行头和数据类型
时间: 2023-05-30 20:02:12 浏览: 66
以下是一个Python代码示例,可随机生成数据并定义行头和数据类型:
```python
import random
import string
# 定义行头和数据类型
headers = ['Name', 'Age', 'Gender', 'City']
data_types = ['string', 'int', 'string', 'string']
# 随机生成数据
data = []
for i in range(10):
row = []
for j in range(len(headers)):
if data_types[j] == 'string':
value = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
elif data_types[j] == 'int':
value = random.randint(1, 100)
else:
value = ''.join(random.choice(string.ascii_uppercase) for _ in range(5))
row.append(value)
data.append(row)
# 输出结果
print(headers)
for row in data:
print(row)
```
在上面的代码中,我们首先定义了行头和数据类型。然后使用for循环来生成随机数据。对于每个数据类型,我们使用了不同的随机函数和参数来生成相应类型的数据。最后,我们将生成的数据打印出来,以验证结果是否符合预期。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)