Csv writer puts quotes around each line

I am trying to combine 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 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, every line in the csv output is fully double-quoted, I try to use instead list.append(row)

, but it doesn't matter since the string is double-quoted. Is there a way to avoid this?

EDIT:

Here's an example of a source file:

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

      

And the output is:

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

      

+3


source to share


2 answers


I think this will save you the quotes caused by not telling the created csv.reader

one that the delimiters in the input file are "|"

characters and not the default, which are 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 the first file, skip its header row
            if not first_file:
                next(reader)
                first_file = False
            writer.writerows(reader)

      

+4


source


The csv module automatically adds double quotes at the end of each line. Without knowing exactly what your csv file looks like, try this:



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, skipinitialspace=True)
        with open(filewriter_lbsum,"a") as f:
            writer = csv.writer(f,delimiter = "|", quoting=csv.QUOTE_NONE)
            #check to see if it 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)

      

0


source







All Articles