Bonjour tout le monde.
J'essaie de récupérer dans un fichier txt tous les paragraphes de mon fichier "13.txt" qui contiennent le mot clé "Fig.".
J'ai jusque là, pu isoler et afficher les paragraphes qui m'intéressent. J'ai cependant un problème pour écrire l’affichage dans un fichier txt. Tout ce que j'ai dans mon fichier Fig.txt c'est "NONE".
Si quelqun peu m'aider.
Merci
Voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 import re with open('13.txt') as file: data = file.read() paragraphs = [item for item in data.split('\n\n') if item] rawParagraphs = [] for paragraph in paragraphs: newParagraph = [] lines = paragraph.split('\n') for line in lines: cols = [item for item in line.split(' ') if item] newParagraph.append(cols) # Find max cols of pg maxcol = max([len(line) for line in newParagraph]) # Patch lines for index, line in enumerate(newParagraph): if len(line)< maxcol: if lines[index].startswith(' '): for i in range(maxcol-len(line)): line.insert(0, '') else: for i in range(maxcol-len(line)): line.append('') newParagraph[index]= line rawParagraph = [] for i in range(maxcol): for j in range(len(newParagraph)): rawParagraph.append(newParagraph[j][i]) rawParagraph = ' '.join(rawParagraph).replace('- ','') rawParagraphs.append(rawParagraph) references = [paragraph for paragraph in rawParagraphs if re.search('Fig.', paragraph)] for index, reference in enumerate(references): data = print(index, reference, '\n') textcle = open("Fig.txt","w",encoding="utf8") textcle.write(str(data)) textcle.close
Partager