If I understand correctly what you want is to read.txt file and want to count how many words started with the letter 'a'.
For this you could use the method. readlines(), match a variable and for each line that is read, you use one of the functions of the strings that is to be used as a list
I think the code makes it easier to understand:
contador = 0 #Variável que utilizaremos para contar
with open('arquivo.txt','r',encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line[0].lower() in 'a':
#funcionalidade de lista em strings
contador += 1
print(contado)
Do not forget to put the encoding when opening the file, otherwise it will use the default enconding and with is the accent is different
If you also want to count phrases that include accents, you can install the library
from unidecode import unidecode
lines=[]
contador = 0
with open('arquivo.txt','r',encoding='utf-8') as f:
for c in f.readlines():
lines.append(unidecode(c))
for line in lines:
if line[0].lower() in 'a':
contador += 1
print(contador)
I managed to do so because I do not know method to take accentuation from list and this was the easiest way for me
If you’re having trouble installing the unidecode library, go to the terminal, access the scripts directory (interpreter for your IDE) and put:
Pip install unidecode
For example with pycharm in windows:
cd C:\Users\<nome do usuário que está usando>\<pasta onde guarda seus programas>\venv\Scripts && pip install unidecode
Now if you wish to show the word itself that has repeated, you can do so:
repetido =[]
with open('arquivo.txt','r') as final:
ultimo = final.readlines()
if ultimo[len(ultimo)-1] != '.':
with open('bans.txt','a') as u:
u.write('\n.')
with open('arquivo.txt','r',encoding='utf-8') as f:
lines = f.readlines()
p = []
for line in lines:
t = 0
repetido = [f'{line}']
for c,v in enumerate(lines):
repetido[0]
if v == repetido[0]:
if t!=0:
if c not in p:
print(v,c)
p.append(c)
else:
t+=1
I hope I’ve helped!
If you have any questions about a function or do not understand my procedure, just send a comment and I will try to answer it as soon as possible
NOTE:This program differentiates words with uppercase and lowercase letters, with or without accentuation and also works better being everything in topics
My list returns strings, in this other question the list is with integers, it would be the same process?
– LexusRX
Yes, it’s the same thing. You may have to make a small modification or other but it should be simple. Test the code to see. I arrived at this question with a brief search on the site, but if you do a better search here you will probably find an even more similar. :-)
– Luiz Felipe
https://stackoverflow.com/questions/25798674/python-duplicate-words
– Paulo Vieira