BearHunt
BearHunt

Reputation: 135

How can I append new lines from 1 CSV to another existing CSV

In the code below the program reads a CSV that already exists. A second CSV file is presented and the code needs to extract and append the new lines in the output CSV. The code is working properly. However, I want to know if it is possible to write a cleaner/nicer piece of code.

How can I clean up the code better?

input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'

with open(output_file, 'r') as out_file:
    lines_out = []
    for line in out_file:
        lines_out.append(line)

with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
    for line in in_file:
        if line in lines_out:
            continue
        else:
            out_file.write(line)

Content cache_2.csv:

"line 5"
"line 6"
"line 7"
"line 8"

Content of output.csv:

"line 1"
"line 2"
"line 3"
"line 4"

Upvotes: 1

Views: 90

Answers (3)

JGFMK
JGFMK

Reputation: 8904

open('./file1.csv', 'a').writelines(open('./file2.csv', 'r'))

This appends file2.csv to the end of file1.csv.. It'll create file1.csv if not found first time too.

Upvotes: 0

Riccardo Bucco
Riccardo Bucco

Reputation: 15364

Here is a simple solution:

with open(output_file, "r+") as out_file, open(input_file, "r") as inp_file:
    out_content = out_file.readlines()
    out_file.writelines([line for line in inp_file.readlines() if line not in out_content])

Upvotes: 2

Vaibhav Jadhav
Vaibhav Jadhav

Reputation: 2076

Try this out. It will work.

input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
    lines_out = []
    for line in out_file:
        lines_out.append(line)
    for line in in_file:
        if line not in lines_out:
            out_file.write(line)

a+ mode allows you to read as well as edit the file. So no need of first loop to read the file.

To reduce further:-

input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
    lines_out = []
    [lines_out.append(line) for line in out_file]
    [out_file.write(line) for line in in_file if line not in lines_out]

Upvotes: 1

Related Questions