I have a problem while I'm doing my assignment with python.
I'm new to python so I am a complete beginner.
Question: How can I merge two files below?
s555555,7
s333333,10
s666666,9
s111111,10
s999999,9
and
s111111,,,,,
s222222,,,,,
s333333,,,,,
s444444,,,,,
s555555,,,,,
s666666,,,,,
s777777,,,,,
After merging, it should look something like:
s111111,10,,,,
s222222,,,,,
s333333,10,,,,
s444444,,,,,
s555555,7,,,,
s666666,9,,,,
s777777,,,,,
s999999,9,,,,
Thanks for reading and any helps would be appreciated!!!
Here are the steps you can follow for one approach to the problem. In this I'll be using FileA
, FileB
and Result
as the various filenames.
One way to approach the problem is to give each position in the file (each ,
) a number to reference it by, then you read the lines from FileA
, then you know that after the first ,
you need to put the first line from FileB
to build your result that you will write out to Result
.
Open FileA
. Ideally you should use the with statement
because it will automatically close the file when its done. Or you can use the normal open()
call, but make sure you close the file after you are done.
Loop through each line of FileA
and add it to a list
. (Hint: you should use split()
). Why a list? It makes it easier to refer to items by index as that's our plan.
Repeat steps 1 and 2 for FileB
, but store it in a different list variable.
Now the next part is to loop through the list of lines from FileA
, match them with the list from FileB
, to create a new line that you will write to the Result
file. You can do this many ways, but a simple way is:
- First create an empty list that will store your results (
final_lines = []
)
- Loop through the list that has the lines for
FileA
in a for
loop.
You should also keep in mind that not every line from FileA
will have a corresponding line in FileB
. For every first "bit" in FileA
's list, find the corresponding line in FileB
's list, and then get the next item by using the index()
. If you are keen you would have realized that the first item is always 0
and the next one is always 1
, so why not simply hard code the values? If you look at the assignment; there are multiple ,
s so it could be that at some point you have a fourth or fifth "column" that needs to be added. Teachers love to check for this stuff.
- Use
append()
to add the items in the right order to final_lines
.
Now that you have the list of lines ready, the last part is simple:
- Open a new file (use
with
or open
)
- Loop through
final_lines
- Write each line out to the file (make sure you don't forget the end of line character).
- Close the file.
If you have any specific questions - please ask.