Stacey
Stacey

Reputation: 5107

Removing whitespace and header from stacked csv file

I have two .csv files (csv1 and csv2) which look like:

csv1:

Quarter  Body   Total requests  Requests Processed
Q3 2019     A               93                  92
Q3 2019     B              228                 210
Q3 2019     C              180                 178
Q3 2019     D               31                  31

csv2:

Quarter  Body   Total Requests  Requests Processed
Q2 2019     A               50                  50
Q2 2019     B              191                 177
Q2 2019     C              186                 185
Q2 2019     D               35                  35

I have stacked the two .csv files using the following code:

with open(pathCsv2, 'r') as f1:
    Csv2 = f1.read()

with open(pathCsv1, 'a') as f2:
    f2.write('\n')
    f2.write(csv2)

This gives me the output:

Quarter  Body   Total requests  Requests Processed
Q3 2019     A               93                  92
Q3 2019     B              228                 210
Q3 2019     C              180                 178
Q3 2019     D               31                  31

Quarter  Body   Total Requests  Requests Processed
Q2 2019     A               50                  50
Q2 2019     B              191                 177
Q2 2019     C              186                 185
Q2 2019     D               35                  35

Is there a way to remove the white space line in inbetween the joined files(I think that the white space is associated with the final line in csv1) and the second header, so the final output would look like:

Quarter  Body   Total requests  Requests Processed
Q3 2019     A               93                  92
Q3 2019     B              228                 210
Q3 2019     C              180                 178
Q3 2019     D               31                  31
Q2 2019     A               50                  50
Q2 2019     B              191                 177
Q2 2019     C              186                 185
Q2 2019     D               35                  35

Upvotes: 1

Views: 61

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Skip the header using next

Ex:

with open(pathCsv2, 'r') as f1:
    next(f1) #Skip first line
    Csv2 = f1.read()

with open(pathCsv1, 'a') as f2:
    f2.write('\n')
    f2.write(Csv2)

Upvotes: 1

Related Questions