我有一个.txt文件,其中包含网站列表。所有网站都列为:
web1=www.facebook.com
web1pass=password123
web2=www.instagram.com
web2pass=password123
web3=www.youtube.com
web3pass=password123
web4=www.twitter.com
web4pass=password123
我要数清所有的";web";有序列号的单词。我的代码是:
with open(datafile, "r") as file:
data = file.readlines()
web_count = 0
for line in data:
if line.split("=")[0] == "web": # I thought using web* might work.
web_count = web_count + 1
print(f"{web_count} websites found.")
有没有一种方法可以编写一个计算所有网站的代码?最好没有任何模块。提前谢谢。
您可以使用regex
获取子字符串,如web1
、web2
,然后创建set
以删除任何重复项,最后将其传递给内置len
以获取计数
import re
>>> len(set(re.findall('webd+', line)[0] for line in f.readlines())) #f is fileBuffer
#output:
4
如果你不想使用regex,你可以用字符串方法来做,但这将是不必要的复杂。
这应该有效:
with open(datafile, "r") as file:
data = file.readlines()
web_count = 0
for line in data[::2]:
if line.startswith("web"):
web_count += 1
print(f"{web_count} websites found.")
只通过第0、2、4行。。。(数据[::2](。
由于您正在绑定以检查是否"web";出现在文本的开头。你可能想选startwith
。
示例:
text = "web132=blabla.com"
result = text.startswith('web')
print(result) #True
我不确定序列号。如果你想确定文本在"数字"后面有一个数字序列;web";字符串,那么您肯定应该使用Regex。一个简单的正则表达式就可以做到这一点,比如:(?:web)d+
这应该做到:
with open(datafile, "r") as file:
data = file.readlines()
web_count = 0
for line in data:
target = line.split("=")[0]
is_web = target[:3]=="web" # check if the first 3 is web
is_sequence = target[3:].isnumeric() # check if the following is a number
if is_web and is_sequence:
web_count = web_count + 1
您可以使用Regex、re
、module。它包含在标准python库中。
您可以使用search
函数在字符串中查找'web'
。
代码:
import re
with open(datafile, "r") as file:
data = file.readlines()
web_count = 0
for line in data:
if re.search('web', line):
web_count = web_count + 1
print(f"{web_count} websites found.")
输出:
8 websites found.
如果你不想匹配密码。
代码:
import re
with open(datafile, "r") as file:
data = file.readlines()
web_count = 0
for line in data:
if re.search('webd+=', line):
web_count = web_count + 1
print(f"{web_count} websites found.")
输出:
4 websites found.