Python Фильтрация - PullRequest
       15

Python Фильтрация

0 голосов
/ 09 июля 2020

Я пытаюсь найти способ взять исходные файлы, которые находятся во входном каталоге, и отфильтровать их, чтобы отображались только строки, содержащие определенное слово. Сейчас исходный файл полностью скопирован и вставлен в мой шаблон. Я хотел бы отфильтровать столбец A для всего, что содержит «Вниз *». Мне сложно понять, как это сделать, из-за того, что пустые ячейки и заголовки не начинаются до строки 4, и мне нужно сохранить пустые ячейки и информацию в строках 1-3, исходные файлы настроены, например, следующим образом :

Column A                  Column B
empty                     Data
empty                     Data
empty                     Data
Direction (Header)        Time (Header)
empty                     Second
Down (Data starts here)   0.00.09
Up                        0.00.10
Down                      0.00.11
Up                        0.00.12
Left                      0.00.12
Down                      0.00.13


Filtered out it would look like:

Column A                  Column B
empty                     Data
empty                     Data
empty                     Data
Direction (Header)        Time (Header)
empty                     Second
Down                      0.00.09         
Down                      0.00.11
Down                      0.00.13


import openpyxl as xl; 
import os
 
input_dir = 'C:\\Python\\Reports'
output_dir = 'C:\\Python\\Reports\\output'
template = os.path.join(input_dir, 'Template.xlsx')
summaryFile = 'FinalReport_1a_'

SearchName = 'Down*'




schedule_index = 0
schedules=['Nm', 'S01', 'S02']
  
files = [file for file in os.listdir(input_dir)
         if os.path.isfile(file) and file.startswith('RRC')]
  
for file in files:
    input_file =  os.path.join(input_dir, file)
    wb=xl.load_workbook(input_file)
    ws=wb.worksheets[1]
      
    # Open template
    wb2 = xl.load_workbook(template) 
    ws2 = wb2.worksheets[1] 
     
    # calculate total number of rows and  
    # columns in source excel file 

    mr = ws.max_row 
    mc = ws.max_column 
     
    # copying the cell values from source  
    # excel file to destination excel file 
    for i in range (1, mr + 1): 
        for j in range (1, mc + 1): 
             
    # reading cell value from source excel file 
            c = ws.cell(row = i, column = j) 
    # Cells for source data to pasted inside Template
            ws2.cell(row = i+8, column = j+1).value = c.value 
     
    # saving the destination excel file 
    
    output_file =os.path.join (output_dir, f"{summaryFile}_{schedules[schedule_index]}.xlsx")
    schedule_index += 1
    wb2.save(output_file)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...