add row to a csv file with python

Question:

I have two csv files that each have 2 columns, one of them being the date. I want to add the second column of the second file to the first file, resulting a file with 3 columns.

I did it by creating a new file and appending the data to it this way:

import csv

coinsfile = open('total-bitcoins.csv', newline='')
pricesfile = open('market-price.csv', newline='')

coins = csv.reader(coinsfile, delimiter=',')
prices = csv.reader(pricesfile, delimiter=',')

with open('result.csv', 'w') as res:
    for coin_row, price_row in zip(coins, prices):
        line = str(coin_row[0]) + ',' + str(coin_row[1]) + ',' + str(price_row[1])
        res.append(line)

The code runs without any errors but the result is a csv file which is completely empty.

Where am I making the mistake, or is there a better way to do this job?

Asked By: Reza Keshavarz

||

Answers:

res is a file handle, so the append method doesn’t apply to it. So there’s an attribute error while the output file is opened, which results in an empty output file (or, yes, one of the input files is empty, ending zip immediately, but this answer explains how to fix the next issues)

A quickfix would be:

res.write(line+"n")

but the best way would be to flatten the result of zip and feed it to a csv.writer object (using a comprehension to generate each row by addition of both input csv rows)

import csv

with open('result.csv', 'w', newline="") as res, open('total-bitcoins.csv', newline='') as coinsfile, open('market-price.csv', newline='') as pricesfile:
    coins = csv.reader(coinsfile)
    prices = csv.reader(pricesfile)
    cw = csv.writer(res)
    cw.writerows(coin_rows+price_row for coin_row, price_row in zip(coins, prices))

note that newline="" is required when writing your files (Python 3) to avoid the infamous blank line “bug” when running windows

I have added the input files in the with statement to ensure that the inputs are closed when exiting it. And also removed the delimiter parameter as comma is the default.

The easiest way to satisfy this need would be using a library like pandas. Using pandas, adding a column to an existing file would be as easy as loading the file into a dataframe, and adding the required column to it in just one line.

Adding can be done by mere assignment, or through join/merge methods.

Answered By: Reza Keshavarz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.