简体   繁体   English

从许多文本文件中复制选择的行并粘贴到新文件中

[英]Copy select lines from many text files and paste to new file

I'm new to Python and trying to use it to do what I think should be a very simple task. 我是Python的新手,尝试使用它来完成我认为应该非常简单的任务。 I have a folder with many .log files, which each have many lines of data. 我有一个包含许多.log文件的文件夹,每个文件都有很多行数据。 I want to copy the lines which only contain a certain key word, and paste every line from each file in to one master file that I can open in excel. 我想复制仅包含某个关键字的行,然后将每个文件中的每一行粘贴到一个我可以在excel中打开的主文件中。 I've been searching for an answer, and I just can't quite seem to get anything to work. 我一直在寻找答案,但似乎还无法解决任何问题。

This should do what you need. 这应该做您需要的。 Put file with this code in the directory where you have your .log files, replace KEYWORD with what you are actually looking for, and run it. 将具有此代码的文件放在您的.log文件所在的目录中,将KEYWORD替换为实际要查找的内容,然后运行它。

import os
theKeyword = 'KEYWORD'
directory = '.' 
with open('output.csv', 'w') as out:
    for file in os.listdir(directory):
        if file.endswith(".log"):
            with open(file, 'r') as f:
                for line in f:
                    if theKeyword in line:
                        out.write(line)

As suggested, you can use glob instead of os.listdir : 根据建议,您可以使用glob代替os.listdir

from glob import glob
with open('output.csv', 'w') as out:
    for file in glob('*.log'):
        with open(file, 'r') as f:
            for line in f:
                if 'KEYWORD' in line:
                    out.write(line)

The code can be even a bit simpler if you use fileinput module: 如果使用fileinput模块,代码甚至可以更简单fileinput

from glob import glob
import fileinput
with open('output.csv', 'w') as out:
    for line in fileinput.input(glob('*.log')):
        if 'KEYWORD' in line:
            out.write(line)

Another variation of the 'grep in Python' thing: “ Python中的grep”的另一种形式:

from glob import glob
import fileinput
with open('output.csv', 'w') as out:
    out.writelines(line for line in fileinput.input(glob('*.log')) if 'KEYWORD' in line)

In the above snippet, if you remove fileinput.input 's argument, then it will process sys.argv[1:] , so you can run your script with file names as parameters. 在以上代码段中,如果删除fileinput.input的参数,则它将处理sys.argv[1:] ,因此您可以使用文件名作为参数来运行脚本。

In case you'd like to search for files recursively in subdirectories of a directory, you should have a look at os.walk function. 如果要在目录的子目录中递归搜索文件,则应查看os.walk函数。

If you have a Linux/Unix/Mac box, or if you have Cygwin installed on a Windows box, the same can be achieved a bit easier using shell tools: 如果您使用的是Linux / Unix / Mac机器,或者如果您在Windows机器上安装了Cygwin ,则可以使用Shell工具轻松实现上述目的:

$ cat *.log| grep KEYWORD >output.csv
import os

outfile = open("outfile.txt", "w")
temp_list = []
for cur_file in os.listdir("."):
    if cur_file.endswith(".log"):
        for line in open(cur_file, "r").readlines():
            if "KEYWORD" in line:
                outfile.write(line)
outfile.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图从一个目录中的许多文本文件中复制一组特定的字符串并将它们粘贴到一个新的文本文件中 - Trying to copy a set of specific strings from many text files in a directory and paste them in a new text file 将特定行从多个文本文件复制到Excel文件 - Copy specific lines from multiple text files to an excel file 复制并粘贴多个txt文件的内容以创建一个大文件 - copy and paste the contents of many txt files to create one big file 从文件一次复制x行,然后将其粘贴到Python的(^ v)中 - Copy x lines at a time from a file, then paste them in (^v) in Python 从匹配第一个元素的两个不同CSV文件中复制行,然后在Python中一个接一个地粘贴到第三个CSV文件中 - Copy lines from two different CSV files matching first element and paste it in 3rd CSV file one after another in Python Python 3 文本文件 - 我想将用户输入添加到文件 1 中每行文本的末尾,然后将新行复制到文件 2 中 - Python 3 text files - I want to add add user input to the end of each line of text in file 1 then copy the new lines into file 2 从网页复制文本并将其粘贴到txt文件或csv文件中 - Copy and paste text from webpage to txt file or csv file 从许多 csv 文件中选择行并创建新文件 - Selecting lines from many csv files and creating new files 文本文件和换行 - Text Files, and New Lines 使用 python 从文本文件复制并粘贴到 csv - Copy from text file and paste onto csv using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM