python: merge two csv files

2024/10/6 6:22:41

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!!!

Answer

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.

  1. 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.

  2. 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.

  3. 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:

  1. First create an empty list that will store your results (final_lines = [])
  2. 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.

  1. 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:

  1. Open a new file (use with or open)
  2. Loop through final_lines
  3. Write each line out to the file (make sure you don't forget the end of line character).
  4. Close the file.

If you have any specific questions - please ask.

https://en.xdnf.cn/q/118865.html

Related Q&A

Generate a custom formated string with python

I have a Javascript code that generates a string (similar to uuid) stringHere it is the js code: var t = "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx", i = (new Date).getTime(); return e = t.repla…

PySide-6.6: clicked signal sends extra boolean argument to slot

I just upgraded PySide6 (PySide6==6.6.0 from 6.2.2) and a new behavior is wreaking havoc with my GUI program. Every place where I have a clicked signal, the hooked up slot is receiving an extra bool==F…

SMTPConnectError when using Django

Im using django-registration for handling of users registration. I tried to signup in order to test it, after testing it, I got this errorSMTPConnectError at /accounts/register/Being trying to find a s…

how to have a single search API using path parameters (No form used)

I have been using this view for searching a word as:db refers mongo connection (just for ref)@app.route(/) def index():return render_template(index.html)@app.route(/words-<word>, methods=[GET, PO…

understanding the return type of anonymous function lambda

I am trying to understand how can lambda function be used. def adder_func(a, b):return a + bprint(adder_func(4, 5))# trying with lambda print(list(lambda a, b: a + b))When trying to use lambda as a add…

What is the meaning of Failed building wheel for flask-mysqldb in pip3 install?

I have a MacBook Air with macOs Sonoma 14.0 and when I write in the terminal $ pip3 install flask-mysqldbI get the error:How can I fix this?

How do I install pygame for a new version of idle? (Windows) [duplicate]

This question already has answers here:Error on install Pygame(3.9) install (Win10) [duplicate](1 answer)Unable to install pygame on Python via pip (Windows 10)(6 answers)Closed 3 years ago.I installed…

How to parse a dynamic dom element?

I want to make a parser for scraping price, however I cant find the working method of parsing innerHTMLI dont know why, but selenium (getAttribute(innerHTML)), phantomjs (page.evaluation function(){ret…

Get a string in Shell/Python with subprocess

After this topic Get a string in Shell/Python using sys.argv , I need to change my code, I need to use a subprocess in a main.py with this function :def download_several_apps(self):subproc_two = subpro…

Python Indentation Error when there is no indent error [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.Is it me or the interpreter? I see no indentation error in my cod…