Python: `paste multiple (unknown) csvs together

2024/10/11 20:22:48

What I am essentially looking for is the `paste' command in bash, but in Python2. Suppose I have a csv file:

a1,b1,c1,d1
a2,b2,c2,d2
a3,b3,c3,d3

And another such:

e1,f1
e2,f2
e3,f3

I want to pull them together into this:

a1,b1,c1,d1,e1,f1
a2,b2,c2,d2,e2,f2
a3,b3,c3,d3,e3,f3

This is the simplest case where I have a known number and only two. What if I wanted to do this with an arbitrary number of files without knowing how many I have.

I am thinking along the lines of using zip with a list of csv.reader iterables. There will be some unpacking involved but seems like this much python-foo is above my IQ level ATM. Can someone suggest how to implement this idea or something completely different?

I suspect this should be doable with a short snippet. Thanks.

Answer
file1 = open("file1.csv", "r")
file2 = open("file2.csv", "r")for line in file1:print(line.strip().strip(",") +","+ file2.readline().strip()+"\n")

Extendable for as many files as you wish. Just keep adding to the print statement. Instead of print you can also have a append to a list or whatever you wish. You may have to worry about length of files, I did not as you did not specify.

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

Related Q&A

Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter page. But this only works when the login is successful. How can i do the same thing when some error occurs?? Ps: I…

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …

SPIDEV on raspberry pi for TI DAC8568 not behaving as expected

I have a Texas Instruments DAC8568 in their BOOST breakout board package. The DAC8568 is an 8 channel, 16bit DAC with SPI interface. The BOOST package has headers to connect it to my raspberry pi, an…

Tensorflow: Simple Linear Regression using CSV data

I am an extreme beginner at tensorflow, and i was tasked to do a simple linear regression using my csv data which contains 2 columns, Height & State of Charge(SoC), where both values are float. In …