python - searching a .txt file by list of words

-4

i designed a destop application in python.

With one button i get a list of word from an excel file. With the other button i want to search a txt document by this list of words. Always if one of the words from the list is found in the text i want the line it is in to be written to another txt document.

Here is my code the button to get the words (work_file_with_excel) works but the button to search the file an return lines (search_for_lines) doesn't. If the button is pressed it always returns: Process finished with exit code -1073740791 (0xC0000409)

Here is my code:

#import numpy as np
#import xlrd
#import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
import random
import sys


bpa_file ='Technische Gebäudeausrüstung'
gewerke_file ='Gebäudeautomation'


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class filedialogdemo(QWidget):
    def __init__(self, parent = None):
        super(filedialogdemo, self).__init__(parent)

        layout = QVBoxLayout()

        self.button_1 = QPushButton("Suchliste")
        self.button_1.clicked.connect(self.work_file_with_excel)
        layout.addWidget(self.button_1)

        self.button_2 = QPushButton("Wortsuche")
        self.button_2.clicked.connect(self.search_for_lines)
        layout.addWidget(self.button_2)

        self.setLayout(layout)
        self.setWindowTitle("Lea\'s Program")

    def work_file_with_excel(self):
        print('fängt an')
        if bpa_file == 'Technische Gebäudeausrüstung':
            file_name_tga = '2018_10_26_Liste zum Prüfen_Arbeitsdatei.xlsx'
            xls = pd.ExcelFile(file_name_tga, encoding_override="utf-8")
            xls.sheet_names
            ['Allgemein', 'Nebenleistungen', 'Besondere Leistungen','von Prüfliste', 'Hersteller']
            if gewerke_file == 'Gebäudeautomation':
                df_1 = pd.read_excel(file_name_tga, sheet_name="Allgemein")
                allgemein_1 = list (df_1['allgemein'])
                print (allgemein_1)
            else:
                print ('anderes gewerk')
        elif bpa_file == 'Rohbau':
            print ('Rohbau')


    def search_for_lines(filename, words_list):
        words_found = 0
        with open(filename) as db_file:
            for line_no, line in enumerate(db_file):
                if any(word in line for word in words_list):
                    sys.stdout = open('Ergebnisse.txt', 'a')
                    print(line_no, ':', line)
                    words_found += 1
        return words_found


    while True:
        fqWords = ['dreigeschossig','bauseits']
        search_for_lines("test.txt", fqWords)
        break


def main():
    app = QApplication(sys.argv)
    ex = filedialogdemo()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
python
list
pyqt5
asked on Stack Overflow Nov 10, 2018 by Mady

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0