从文本文件中搜索单词列表,并使用python打印前三行

时间:2019-07-17 18:41:48

标签: python-3.x

我有一个文本文件,其中我必须访问特定的标题并访问标题下的第一行。我遇到一个问题时可以对一个标题进行处理。

我已经成功完成了一个标题。但是在做单词列表时我做不到。  我能够做到一个方向 数据= ['工作:']

在这种情况下,我无法做到这一点。 数据= ['work:','test:','ride:']

In the text file the data is like below:
work:
'500'
'ast'
'800'

test:
'tim'
'200'
'300'

ride:
'mic'
'100'
'657'
import math
import os
import glob
import re
import sys



sys.path.append('C:/Documents/tes')



def read(file,Data,res,outputline):
    with open(file,'r') as f:
        stc_file = os.path.basename(file)
        for line in f:
            if Data in line:
                line = f.readlines()
                return line[outputline]

fls = []
src_dir = r'C:/Documents/tes'
for root, dirs, files in os.walk(src_dir):
    for filename in files:
        if not filename.endswith('.txt'):
            continue
        filepath = os.path.join(root, filename)
        fls.append(filepath)
        result = []
        Data = ['work:','test:','ride:']
        for file in fls:
            result=read(file,Data,result,0).split()+read(file,Data,result,1).split()+read(file,Data,result,2).split()
The above code is working for one heading,but for multiple headings i was not able to do.

['500','ast','800']
['tim','200','300']
['mic','100','657']
This above expected output .

1 个答案:

答案 0 :(得分:0)

如果要查找的三行数据(不确定是否想要更多,还是任意数目?)中的每行都用单引号引起来,并且我是否正确理解了目标,则此脚本将执行您所要求的操作...

import os

src_dir = os.getcwd()  # or whatever path you want
keywords = ['work:', 'test:', 'ride:']

result = []

for root, dirs, files in os.walk(src_dir):
    for filename in files:
        if filename.endswith('.txt'):
            path = os.path.join(root, filename)
            try:
                fh = open(path, 'r')
                lines = [l.strip("'") for l in fh.read().splitlines()]

                for i in range(len(lines)):
                    if lines[i] in keywords:
                            result.append(' '.join(lines[i+1:i+4]).split())

            except Exception as e:
                print('Something went wrong.')
                print(e)
                continue

print(result)