2

I would like to take values from two csv files and put that in a single CSV file.

Please refer the Data in those two csv files:

CSV 1:

|   | Status   | P  | F | B | IP | NI | NA | CO | U |
|---|----------|----|---|---|----|----|----|----|---|
| 0 | Sanity 1 | 14 |   |   |    |    |    | 1  |   |
| 1 | Sanity 2 | 13 |   | 1 |    |    |    | 1  |   |
|   |          |    |   |   |    |    |    |    |   |

CSV 2:

|   | Status     | P   | F | B | IP | NI | NA | CO | U |
|---|------------|-----|---|---|----|----|----|----|---|
| 0 | P0 Dry Run | 154 | 1 |   |    | 1  |    |    | 5 |
|   |            |     |   |   |    |    |    |    |   |
|   |            |     |   |   |    |    |    |    |   |

Code: I tried with following code:

filenames = glob.glob ("C:\\Users\\gomathis\\Downloads\\To csv\\*.csv")
wf = csv.writer(open("C:\\Users\\gomathis\\Downloads\\To 
csv\\FinalTR.csv",'wb'))

for f in filenames:
    rd = csv.writer(open(f,'r'))
    next(rd)
    for row in rd:
        wf.writerow(row)

Actual result: While trying with above code, I didn't get the values from those above CSV files.

Expected result: I need that two files needs to be added in a single csv file and to be saved locally.

Modified code:

filenames = glob.glob ("C:\\Users\\gomathis\\Downloads\\To csv\\*.csv") wf = csv.writer(open("C:\\Users\\gomathis\\Downloads\\To csv\\FinalTR.csv",'w')) print(filenames)

for f in filenames:

  rd = csv.reader(open(f,'r', newline=''))
  next(rd)
  for row in rd:
      wf.writerow(row)

Latest result: I got the below result after modifying the code. And I didn't get the index like status P, F,B,etc. Please refer the latest result.

| 0 | P0 Dry Run - 15/02/18 | 154 | 1 | | | 1 | | | 5 | |---|--------------------------------|-----|---|---|---|---|---|---|---| | | | | | | | | | | | | 0 | Sanity in FRA Prod - 15/02/18 | 14 | | | | | | 1 | | | | | | | | | | | | | | 1 | Sanity in SYD Gamma - 15/02/18 | 13 | | 1 | | | | 1 | |

8
  • 1
    Possible duplicate of Merging 2 csv files Commented Apr 18, 2018 at 12:01
  • Are you using Python 2 or 3? If Python 2 your open csv files for reading should be open(f, 'rb'). If Python 3, open(f, 'r', newline=''). Writing follows the same pattern. The reason for this special handling for csv files is that the csv module needs to bypass Python's universal newline support. Commented Apr 18, 2018 at 12:16
  • Are you certain your glob is matching your filenames? To check print filenames before your loop. Commented Apr 18, 2018 at 12:22
  • @StevenRumbalski Sorry the late response. I got this result while printing before passing it into loop ['C:\\Users\\gomathis\\Downloads\\To csv\\FinalTR.csv', 'C:\\Users\\gomathis\\Downloads\\To csv\\file1.csv', 'C:\\Users\\gomathis\\Downloads\\To csv\\file2.csv'] Commented Apr 18, 2018 at 15:11
  • Are you using Python 2 or 3? What ends up in FinalTR.csv? Anything? As @Ank points out you do rd = csv.writer(open(f,'r')) when you should to rd = csv.reader(open(f,'r')). You need a csv.reader not a csv.writer in that spot. Also, since your destination file exists in that list you should skip reading it. if f.endswith('FinalTR.csv'):continue Commented Apr 18, 2018 at 15:14

2 Answers 2

1

You need to call the csv reader method over your csv files in the loop.

rd = csv.reader(open(f,'r'))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bro. It is useful.
0
import csv
import glob

dest_fname = "C:\\Users\\gomathis\\Downloads\\To csv\\FinalTR.csv"
src_fnames = glob.glob("C:\\Users\\gomathis\\Downloads\\To csv\\*.csv")
with open(dest_fname, 'w', newline='') as f_out:
    writer = csv.writer(fout)
    copy_headers = True
    for src_fname in src_fnames:
        # don't want to overwrite destination file
        if src_fname.endswith('FinalTR.csv'):
            continue
        with open(src_fname, 'r', newline='') as f_in:
            reader = csv.reader(f_in)
            # header row is copied from first csv and skipped on the rest
            if copy_headers:
                copy_headers = False
            else:
                next(reader) # skip header 
            for row in reader:
                writer.writerow(row)

Notes:

  • Placed your open() in with-statements for automatic closing of files.
  • Removed the binary flag from file modes and added newline='' which is needed for files passed to csv.reader and csv.writer in Python 3.
  • Changed from csv.writer to csv.reader for the files you were reading from.
  • Added a copy_headers flag to enable copying headers from the first file and skipping copying of headers from any files after that.
  • Check source filename and skip it when it matches the destination filename.

4 Comments

Thank you. But I have another clarification, what if I want to select only two csv files from a folder which contains more than 3 csv files.
@gomathisubramanian: What's special about the two names? How do you know the difference manually?
week_1 and week_2 and those are the files where I have the data to make it as a single file
@gomathisubramanian: You could change the end of your glob() argument from '*.csv' to 'week_[1-2].csv'. Or 'week_[1-9]' for weeks one through nine. With a glob that specific you could remove the lines if src_fname.endswith('FinalTR.csv'): continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.