求助:python中,随机生成的“字符串”中如果包含某个“子字符”就输出这个“字符串”应该怎么写?

软件和网站开发以及相关技术探讨
回复
头像
九天星
帖子: 1440
注册时间: 2007-07-14 20:45

求助:python中,随机生成的“字符串”中如果包含某个“子字符”就输出这个“字符串”应该怎么写?

#1

帖子 九天星 » 2021-10-21 23:17

求助:python中,随机生成的“字符串”中如果包含某个“子字符”就输出这个“字符串”应该怎么写?

使用faker模块,随机循环生成1000个字符串,但我只想要这1000个字符串里面包含LINUX字符的字符串,应该怎么写下面这串脚本?请教!

代码: 全选

#!/usr/bin/python3 env
#encoding:utf-8
from faker import Factory
# 生成不同的user-agent
User_Agent = Factory.create()
zi = "Linux"
for i in range(1000):
    if zi in User_Agent:
    print(User_Agent.user_agent())
这样写怎么都不对,应该如何改进?请教。。。
头像
astolia
论坛版主
帖子: 6453
注册时间: 2008-09-18 13:11

Re: 求助:python中,随机生成的“字符串”中如果包含某个“子字符”就输出这个“字符串”应该怎么写?

#2

帖子 astolia » 2021-10-22 14:21

你都不看一下Factory.create()返回的User_Agent到底是个什么东西?
User_Agent.user_agent()返回的才是随机生成的useragent字符串
回复