Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csv writer puts quotes around each row

I'm trying to merge multiple csv files with the same format into one.

merge_list = glob.glob(gndlbsum+"*gndlbsum.csv")
filewriter_lbsum = target_dir+"gndlbsum_master.csv"

#get the list of csv files and set the output file
counter=0
for file in merge_list:
    with open(file,"rU") as csv_file:
        filereader = csv.reader(csv_file)
        with open(filewriter_lbsum,"a") as f:
            writer = csv.writer(f, delimiter = "|")
            #check to see if it's the first file, if it is, add header,
            #otherwise skip first row
            if counter<1:
                for row in filereader:
                    writer.writerow(row)
                    counter+=1
            else:
                header = next(filereader,None)
                for row in filereader:
                    writer.writerow(row)

When I do it like this, each row in the output csv is entirely enclosed with double quotes, I tried to uselist.append(row)instead, but it makes no difference, since the row is enclosed with double quotes. Is there a way to avoid this?

EDIT:

Here is a sample of the source file:

COL1|COL2|COL3
1|2|3
4|5|6

And the output:

"COL1|COL2|COL3"
"1|2|3"
"4|5|6"
like image 599
quickshare Avatar asked Mar 18 '23 14:03

quickshare


1 Answers

I think this will get rid of the quotes which are being caused by not telling the csv.readers being created that the delimiters in the input file are "|" characters rather than default which is "," characters.

merge_list = glob.glob(gndlbsum + "*gndlbsum.csv")
file_writer_lbsum = os.path.join(target_dir, "gndlbsum_master.csv")

# Append each csv file in the list to the output file
first_file = True
for file in merge_list:
    with open(file, "rU") as csv_file:
        reader = csv.reader(csv_file, delimiter="|")
        with open(file_writer_lbsum, "w" if first_file else "a") as f:
            writer = csv.writer(f, delimiter="|")
            # Unless it's the first file, skip its header row
            if not first_file:
                next(reader)
                first_file = False
            writer.writerows(reader)
like image 160
martineau Avatar answered Mar 27 '23 19:03

martineau