简体   繁体   中英

How to extract a string from the file list in Python?

I have a folder with a list of 425 similar files named “00001q1.txt, 00002w2.txt, 00003e3.txt... 00425q1.txt”. Each file contains a line of text between two rows. These rows are constant in all files. I need to extract these lines and save it to output file as column of lines.

This is script which able to loop all the files in a folder, but it doesn't extract desired lines from the list of files to otput file.

#!/usr/bin/python
# Open a file

import re
import os
import sys
import glob

outfile = open("list7.txt", "w")

# This would print all the files and directories (in sorted order) 
full_path = r"F:\files\list"
filelist = sorted(os.listdir( full_path ))
print filelist

# This would scan the filelist and extract desired line that located between two rovs:
# 00001q1.txt:
# Row above line
# line
# Row under line

buffer = []
for line in filelist:
    if line.startswith("Row above line"):
        buffer = ['']
    elif line.startswith("Row under line"):
        outfile.write("".join(buffer))
        buffer = []
    elif buffer:
        buffer.append(line)

# infile.close()
outfile.close()

If I define a single file (for example 00001q1.txt“) instead filelist in the script, then desired line is written to the outfile successfully. What should I do that script scan the list of files?

Thanks in advance.

If I understand well you want to write to list7.txt all needed occurrences:

import os

outfile = open("list7.txt", "w")

full_path = r"F:\files\list"
filelist = sorted(os.listdir(full_path))

with open("list7.txt", "w") as outfile:
    buffer = []
    for filename in filelist:
        with open(os.path.join(full_path, filename), "r") as infile:
            for line in infile.readlines():
                if line.startswith("Row above line"):
                    buffer = ['']
                elif line.startswith("Row under line"):
                    outfile.write("".join(buffer))
                    buffer = []
                elif buffer:
                    buffer.append(line)
            for line in buffer:
                outfile.write(line)

You need to iterate both files and rows in each file:

buffer = []
for fileName in filelist:
    with open(fileName, 'rU') as f:
      for line in f:
        if line.startswith("Row above line"):
          buffer = ['']
        elif line.startswith("Row under line"):
          outfile.write("".join(buffer))
          buffer = []
        elif buffer:
          buffer.append(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM