1

I have 2 files named input.csv (composed of one column count ) and output.csv (composed of one column id).
I want to paste my count column in output.csv, just after the id column.

Here is my snippet :

with open ("/home/julien/input.csv", "r") as csvinput:
    with open ("/home/julien/excel/output.csv", "a") as csvoutput:
        writer = csv.writer(csvoutput, delimiter = ";")

        for row in csv.reader(csvinput, delimiter = ";"):
            if row[0] != "":
                result = row[0]
            else:
                result = ""  

             row.append(result)
         writer.writerow(row)

But it doesn't work.

I've been searching the problem for many hours but I'v got no solution. Would you have any tricks to solve my problem ?
Thanks! Julien

Julien
  • 699
  • 3
  • 14
  • 30
  • Did you try this answer? http://stackoverflow.com/questions/11070527/how-to-add-a-new-column-to-a-csv-file-using-python – 0xBAADF00D Jun 08 '13 at 23:48
  • Thanks for the reply. Yes, I looked at this answer but I guess it's not the same problem I have. – Julien Jun 09 '13 at 00:36
  • I'm not real clear on what you mean. Does `paste -d';' /path/to/input.csv /path/to/output.csv` give you what you want? (on the terminal, not python code) If not, please give a few lines of inputs and expected output. – Kevin Jun 09 '13 at 02:57
  • You need to be more clear on what "doesn't work." In what way is it not working? What is your input? What is your output? What were you expecting? How is the output not matching what you were expecting? etc. – Jeff Mercado Jun 09 '13 at 08:17

3 Answers3

1
  1. Open both files for input.
  2. Open a new file for output.
  3. In a loop, read a line from each, formatting an output line, which is then written to the output file
  4. close all the files
  5. Programmatically copy your output file on top of the input file "output.csv".

Done

STLDev
  • 5,950
  • 25
  • 36
  • Thanks for the reply. Indeed, it's the workflow I'd like to apply. But I don't exactly find the code to make it work. If it doesn't bother you, could you show it to me please ? – Julien Jun 09 '13 at 00:38
1

You need to work with three files, two for reading and one for writing. This should work.

import csv

in_1_name = "/home/julien/input.csv"
in_2_name = "/home/julien/excel/output.csv"
out_name = "/home/julien/excel/merged.csv"

with open(in_1_name) as in_1, open(in_2_name) as in_2, open(out_name, 'w') as out:
    reader1 = csv.reader(in_1, delimiter=";")
    reader2 = csv.reader(in_2, delimiter=";")
    writer = csv.writer(out, delimiter=";")
    for row1, row2 in zip(reader1, reader2):
        if row1[0] and row2[0]:
            writer.writerow([row1[0], row2[0]])

You write the row for each column:

            row.append(result)
            writer.writerow(row)

Dedent the last line to write only once:

            row.append(result)
        writer.writerow(row)
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Thanks. I tried your idea but I had the following error message : Traceback (most recent call last): `File "test.py", line 165, in ` `writer.writerow(row)` `ValueError: I/O operation on closed file` – Julien Jun 09 '13 at 00:31
  • Dedent only one level to the level of `for row in csv.reader(csvinput, delimiter = ";"):`. – Mike Müller Jun 09 '13 at 00:39
  • Ok, That's what I just tried (made the edit in my first message). But my output.csv file is only composed of my _id_ column. The _count_ column of input.csv is not present. – Julien Jun 09 '13 at 01:02
  • I added a solution that works for me with sample data. Try it out. – Mike Müller Jun 09 '13 at 01:26
0

If anyone was given two tables, merging them by using first column of each is very easy. With my library pyexcel, you do the merge just like merging tables:

>>> from pyexcel import Reader,Writer
>>> f1=Reader("input.csv", delimiter=';')
>>> f2=Reader("output.csv", delimiter=';')
>>> columns = [f1.column_at(0), f2.column_at(0)]
>>> f3=Writer("merged.csv", delimiter=';')
>>> f3.write_columns(columns)
>>> f3.close()
chfw
  • 4,502
  • 2
  • 29
  • 32