首页 > 解决方案 > 我有一个 txt 文件,想打印特定的单词

问题描述

这是我的文本文件:

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 

标签: python

解决方案


以下是如何使用该rfind()方法查找'/'字符串中最后出现的索引(如果有的话):

import re

with open('access_log.txt', 'r') as myfile:
    for line in myfile:
        st = line.split()
        print([a[a.rfind('/')+1:] for a in st if a.endswith('.js')])

输出:

['jquery.js']
['jquery.js']
['jquery.jshowoff2.js']
['jquery.min.js']
['jquery.sho.min.js']

推荐阅读