What you have is a file CSV (Comma-separated values, literally "comma separated values"), so a good alternative is to use the module csv
to read it. An initial outline:
import csv
with open('arquivo.txt') as arquivo:
reader = csv.reader(arquivo)
for registro in reader:
cpf = registro[0].strip()
First I open the file inside a block with
, for this assures me that the file is properly closed at the end of the execution.
Then I use csv.reader
to iterate through the records of the file. In this case, every iteration of the for
, the variable registro
will be a list of strings, each element of the list being one of the records (i.e., the first element is CPF, the second is the name, etc).
Then I take the number and use strip()
to remove the spaces from the beginning, as it is not clear in the question whether they are part of the file or if it was a typo. Anyway, now just compare this number with what was typed. It would look something like this:
import csv
cpf_informado = input('informe o CPF: ')
with open('arquivo.txt') as arquivo:
reader = csv.reader(arquivo)
for registro in reader:
cpf = registro[0].strip()
if cpf == cpf_informado:
print('CPF encontrado')
break
else:
print('CPF não encontrado')
If the number is found, I can leave the loop with break
. If not found, he falls else
. And notice that this else
is from for
, not of if
: in Python this is possible, and he falls in else
if the loop is not interrupted by break
(in case, if no CPF is found).
You also said you want to update the name when the number is found. In this case, an alternative is to create another file containing the updated records:
import csv
cpf_informado = input('informe o CPF: ')
with open('arquivo.txt') as arquivo, open('novoarquivo.txt', 'w', newline='') as novo_arquivo:
reader = csv.reader(arquivo)
writer = csv.writer(novo_arquivo)
encontrou = False
for registro in reader:
cpf = registro[0].strip()
if cpf == cpf_informado:
encontrou = True
registro[1] = input('Digite o novo nome: ')
writer.writerow(registro)
if not encontrou:
print('CPF não foi encontrado')
Now you can’t use it break
because I need to continue the loop writing all the records in the new file.
This algorithm was not so good, because in case the CPF is not found, I created the new file for nothing. Then you could save the records in a list, and only create the new file if the CPF is found:
import csv
cpf_informado = input('informe o CPF: ')
with open('arquivo.txt') as arquivo:
reader = csv.reader(arquivo)
encontrou = False
registros = [registro for registro in reader]
for registro in registros:
cpf = registro[0].strip()
if cpf == cpf_informado:
encontrou = True
registro[1] = input('Digite o novo nome: ')
if not encontrou:
print('CPF não foi encontrado')
else:
with open('novoarquivo.txt', 'w', newline='') as novo_arquivo:
writer = csv.writer(novo_arquivo)
for registro in registros:
writer.writerow(registro)
The line registros = [registro for registro in reader]
creates the list registros
, containing the records of the file. It uses the syntax of comprehensilist on, much more succinct and pythonic.
Note that now I only open the new file if the CPF is found. Otherwise, I just print the message.
Just remembering that this solution loads the entire file in memory, so if the file is too large, it is not suitable, requiring there other solutions. But I think that’s beyond the scope of the question.
As for your other problem, of "losing" the changes made, it happens because you always read from the original file and update in the new file (ie you read from the original data, and the new file always has only the last update).
In this case, you could overwrite the file itself at the end:
import csv
cpf_informado = input('informe o CPF: ')
with open('arquivo.txt') as arquivo:
reader = csv.reader(arquivo)
encontrou = False
registros = [registro for registro in reader]
for registro in registros:
cpf = registro[0].strip()
if cpf == cpf_informado:
encontrou = True
registro[1] = input('Digite o novo nome: ')
if not encontrou:
print('CPF não foi encontrado')
else:
with open('arquivo.txt', 'w', newline='') as novo_arquivo:
writer = csv.writer(novo_arquivo)
for registro in registros:
writer.writerow(registro)
I mean, first I read the file and I read the list with the records.
Then off the block with
, I check if the CPF was found, and overwrite the file itself with the new records. This way you don’t "lose" the changes made next time you change another record.
Gabriel, could you clarify better what precisely you tried? If possible, edit your post by presenting at least one of the attempts
– Lucas
https://repl.it/repls/ThankfulPrimarySymbol That?
– Maury Developer
Gabriel, please enter the code and other files as text. Putting as image is not ideal, understand the reasons reading the FAQ. Anyway, the problem is that you always read from Arq.txt and update the new_arq.txt. If you want the original file to have the change, at the end vc should rename the new_arq.txt to Arq.txt
– hkotsubo