Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file of single instances of account number

Tags:

python

string

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.

like image 798
W00t Avatar asked Feb 17 '23 01:02

W00t


2 Answers

with open(filename) as fin, open(newfilename, 'w') as fout:
    fout.writelines(set(fin))
like image 113
John La Rooy Avatar answered Feb 18 '23 13:02

John La Rooy


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.

like image 42
Stephan Avatar answered Feb 18 '23 14:02

Stephan