I have a list of account numbers, separated by line, generated by a log of transactions.
How do I consolidate this list into a file that will have only one occurrence of each account instead of many (where more than one transaction per account has been logged)?
Python is preferred but I can also use C.
with open(filename) as fin, open(newfilename, 'w') as fout:
fout.writelines(set(fin))
I dont know what your log file looks like, but this should work nicely
in python:
file = open('filename.txt', r+)
accountNos = set(file)
file.truncate()
for x in accountNos:
file.write(x)
file.close()
This takes each line out of the file, and stores them in a set. A set is a data structure that only stores unique elements and removes duplicates. In the second for loop you write the contents of that set back to the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With