So, do you have something to find i a text?
Let's use regular expression for that in python.
I got this text... (saved as reg.txt)
Informazioni cronologiche Docente di sostegno Scuola di servizio Anno scolastico Data Funzioni mentali [b114 - Funzioni dell'orientamento] Funzioni mentali [b140 - funzioni dell'attenzione] Funzioni mentali [b144 - funzioni della memoria] Funzioni mentali [b163 - funzioni cognitive di base] Funzioni mentali [b164 - funzioni cognitive di livello superiore] Funzioni mentali [b167 - funzioni mentali del linguaggio] Funzioni mentali [b1720 - Calcolo semplice] Funzioni mentali [b1721 - Calcolo complesso] Funzioni mentali [b1800 - Esperienza del sé] Funzioni mentali [b1802 - Esperienza del tempo] Le performance si riferiscono al contesto: 1 [D110 Guardare P] 1 [D110 Guardare C] 1 [D115 Ascoltare P] 1 [D115 Ascoltare C] 1 [D130 copiare P] 1 [D130 copiare C] 1 [D160 - Focalizzare l'attenzione] 1 [D160 - Focalizzare l'attenzione] 1 [D1630 - Fingere] 1 [D1630 - Fingere] 1 [D1631 - Speculare] 1 [D1631 - Speculare] 1 [D1632 - Ipotizzare] 1 [D1632 - Ipotizzare] 1 [D166 - Leggere] 1 [D166 - Leggere] 1 [D170 - Scrivere] 1 [D170 - Scrivere] 1 [D172 - Calcolare] 1 [D172 - Calcolare] 1 [D175 - risoluzione di problemi] 1 [D175 - risoluzione di problemi] 1 [D177 - Prendere decisioni] 1 [D177 - Prendere decisioni] 1 [D210 - Intraprende un compito singolo] 1 [D210 - Intraprende un compito singolo] Componente b (parte 2) [D250 Controllare il proprio comportamento] Componente b (parte 2) [D250 Controllare il proprio comportamento] Componente b (parte 2) [D310 - Comunicare-ricevere messaggi verbali] Componente b (parte 2) [D310 -
Ok, this is a part of it... but you made an idea of it, right?
I want to extrapulate all the data between square brackets. How?
Let's try this
import re
with open("reg.txt") as file:
text = file.read()
m = re.search('(?<=\\[)\w+', text)
print(m.group(0))
We import the re module, open the file and memorize the text in text string.
Then we memorize the search in the text of a word(w+) that starts after a square \\[ and I have b114 that is nice, but I want all the words after the square and also the rest until the ] ...
I tried this
import re
with open("reg.txt") as file:
text = file.read()
m = re.match(r"[^[]*\[([^]]*)\]", text).groups()[0]
print(m)
Then, searching here and there in stackoverflow I foud this solution
import re
with open("reg.txt") as file:
text = file.read()
m = re.findall(r"[^[]*\[([^]]*)\]", text)
print(m)
But, if you look well there are some strange characters into the output, so my solution is this:
import re
with open("reg.txt",encoding='utf-8') as file:
text = file.read()
m = re.findall(r"[^[]*\[([^]]*)\]", text)
print(m)
For those of you asking, I put encoding='utf-8' inside the with open thing.
Comments
Post a Comment