这是我的文本文件:
10.10.10.10 POST /include/jquery.js HTTP/1.1 233
192.10.10.12 POST /include/jquery.js HTTP/1.1 232
10.10.10.12 POST /node/jquery.jshowoff2.js HTTP/1.1 23e
171.1.1.15 POST /include/jquery.min.js HTTP/1.1 121
10.10.10.10 POST /text/jquery.sho.min.js HTTP/1.1 233
我只想打印包含.js
的文件名。
例如,对于要打印的第一行:
jquery.js
这是我现在拥有的,但它正在打印完整的行
import re
import sys
linenum = 0
substr = '.js'
with open ('access_log.txt', 'rt') as myfile:
for line in myfile:
linenum += 1
if line.find(substr) != -1:
print(line, end=' ')
输出:
10.10.10.10 POST /include/jquery.js HTTP/1.1 233
192.10.10.12 POST /include/jquery.js HTTP/1.1 232
10.10.10.12 POST /node/jquery.jshowoff2.js HTTP/1.1 23e
171.1.1.15 POST /include/jquery.min.js HTTP/1.1 121
10.10.10.10 POST /text/jquery.sho.min.js HTTP/1.1 233
以下是如何使用
rfind()
方法查找字符串中最后出现的'/'
的索引(如果有):输出:
相关问题 更多 >
编程相关推荐