I wish to have to have the first field (Username) from File1 and the second field(Password) output into a third file which is created during the function but I am unable to do it. :(
The format of the files will always be the same which are:
File 1:
Username:DOB:Firstname:Lastname:::
File2:
Lastname:Password
My current code:
def merge(f1,f2,f3):
with open(f3, "a") as outputFile:
with open(f1) as usernameFile:
for line in usernameFile:
line = line[:-3]
username = line.split(':')
outputFile.write(username[0])
with open(f2) as passwordFile:
for line in passwordFile:
password = line.split(':')
outputFile.write(password[1])
merge('file1.txt', 'file2.txt', 'output.txt')
I want the Username from File1 and the Password from File2 to write to File3 with the layout:
Username:Password
Username:Password
Username:Password
Any help would be appreciated. :)
This is pretty close. Be sure no blank line at end of input files, or add code to skip blank lines when you read.
If the files are identically sorted (i.e. the users appear in the same order in both files), use the tip in this answer to iterate over both files at the same time rather than one after the other in your example.
If the files are not identically sorted, first create a dictionary with keys
lastname
and valuesusername
fromfile1
. Then create a second dictionary with keyslastname
and valuespassword
fromfile2
. Then iterate over the keys of either dict and print both values.I would recommend building two dictionaries to represent the data in each file, then write File3 based on that structure:
This will give you two dictionaries that look like this:
To then write this to File 3, simply run through the keys of either dicitonary:
Some things to Note:
If the files don't have all the same values, you'll need to throw in some handling for that (let me know if this is the case and I can toss a few ideas your way
This approach does not preserve any order the files were in
The code assumes that all lines are of the same format. A more complicated file will need some code to handle "odd" lines
This is the minimum change that you would need to do to your code to make it work:
However, this method isn't very good because it reads a file multiple times. I would go ahead and make a dictionary for the second file, with the lastname as a key. Dictionaries are very helpful in these situations. The dictionary can be made apriori as follows:
I just ran the following program using the files:
f1.txt
f2.txt
and got the output:
I did add the last line
merge(...)
and another like which would be used to skip blank lines in the input text, but otherwise, everything should be fine. There wont be any output if themerge(...
function isn't called.Abstract the data extraction from the file i/o, then you can re-use
merge()
with different extraction functions.Files_Iterator
is a With Statement Context Manager that allows multiple synchronous file iteration and ensures the files will be closed. Here is a good start for reading - Understanding Python's "with" statementIts fine to avoid this if you have identically sorted rows in each file. But, if it gets any more complicated than that, then you should be using pandas for this. With pandas, you can essentially do a join, so, no matter how the rows are ordered in each file, this will work. Its also very concise.
Note that you will also have the option to do outer joins. This is useful, if for some reason, there are usernames without passwords or vice versa in your files.