J'essaye d'écrire une fonction python qui lit chaque ligne individuelle d'un fichier texte et recherche un mot-clé: si le mot-clé est dans la ligne je suis essayer d'exporter la ligne dans un nouveau document texte. Cela créerait essentiellement un moyen de filtrer les lignes de texte. Voici ce que j'ai jusqu'à présent:Recherche par mot-clé et exportation vers doc en sortie dans le fichier .txt en Python
#trying to filter and print a line that contains a keyword in a csv or txt
file
import subprocess
import csv
def findandsearch(word):
textfile=open('textHCPC17_CONTR_ANWEB.txt', 'r+')
#openign the selected text file
outputfile=input('CMSOUTPUT.txt')
#classifying an ouput file
word=s'education' #designating a keyword
for line in textfile:
textfile.readline()
if word in textfile: #creating if clause
print('this is a match') #printing that this is match
outputfile.write(word) #I want to write the selected line of the text in the output file
textfile.close() #closing the original file
print(word) #I want to print the results
return #ending function
Toute aide serait appréciée comme je ne suis pas en cours d'exécution dans l'erreur de syntaxe, mais mon fichier de sortie est vide.
Oh, d'accord, cela a du sens. Merci pour l'aide! –