4

I have looked at many solutions for this but cannot find one that works for what I want to do.

Basically I have 2 CSV files:

all.csv

1   Wed Oct 03  41.51093923 41.51093923 41.51093923 41.51093923         
2   Wed Oct 04                          
3   Wed Oct 05  41.43764015 41.43764015 41.43764015             
4   Wed Oct 06  41.21395681 41.21395681 41.21395681             
5   Wed Oct 07  42.07607442 42.07607442 42.07607442             
6   Wed Oct 08  42.0074109  42.0074109  42.0074109              
7   Wed Oct 09  41.21395681 41.21395681                 
8   Wed Oct 10  41.43764015 41.43764015 41.43764015 41.43764015 
9   Wed Oct 11  41.21395681 41.21395681 41.21395681 41.21395681

original.csv

10  Wed Oct 12  41.43764015             
11  Wed Oct 13                  
12  Wed Oct 14  42.07607442 42.07607442 42.07607442     
13  Wed Oct 15  41.43764015 41.43764015 41.43764015 41.43764015
14  Wed Oct 16  41.21395681 41.21395681 41.21395681 41.21395681
15  Wed Oct 17                  
16  Wed Oct 18  42.07607442 42.07607442 42.07607442 

I want to append original.csv to all.csv, by simply taking all rows in original.csv and merging them underneath the last line in all.csv to get:

1   Wed Oct 03  41.51093923 41.51093923 41.51093923 41.51093923         
2   Wed Oct 04                          
3   Wed Oct 05  41.43764015 41.43764015 41.43764015             
4   Wed Oct 06  41.21395681 41.21395681 41.21395681             
5   Wed Oct 07  42.07607442 42.07607442 42.07607442             
6   Wed Oct 08  42.0074109  42.0074109  42.0074109              
7   Wed Oct 09  41.21395681 41.21395681                 
8   Wed Oct 10  41.43764015 41.43764015 41.43764015 41.43764015 
9   Wed Oct 11  41.21395681 41.21395681 41.21395681 41.21395681
10  Wed Oct 12  41.43764015             
11  Wed Oct 13                  
12  Wed Oct 14  42.07607442 42.07607442 42.07607442     
13  Wed Oct 15  41.43764015 41.43764015 41.43764015 41.43764015
14  Wed Oct 16  41.21395681 41.21395681 41.21395681 41.21395681
15  Wed Oct 17                  
16  Wed Oct 18  42.07607442 42.07607442 42.07607442 

As you can see there is no headers for the data and the rows vary in length. This is just an example of the type of files I am working with but I want to get a solution that can work on any CSV.

I am working with Python3 and so far have tried using the pandas library but had no luck.

Any suggestions would be great, thanks.

caaax
  • 450
  • 1
  • 5
  • 15
  • 1
    can you show your attempt at this when using `pandas`? – gold_cy Feb 18 '19 at 14:50
  • 1
    Possible duplicate of [how to combine two data frames in python pandas](https://stackoverflow.com/questions/12850345/how-to-combine-two-data-frames-in-python-pandas) – anky Feb 18 '19 at 14:52
  • Possible duplicate of [how to merge 200 csv files in python](https://stackoverflow.com/questions/2512386/how-to-merge-200-csv-files-in-python) – DavidDr90 Feb 18 '19 at 14:53

1 Answers1

6

You don't need to use pandas. Simply append one csv to another:

with open('original.csv', 'r') as f1:
    original = f1.read()

with open('all.csv', 'a') as f2:
    f2.write('\n')
    f2.write(original)

Output:

1   Wed Oct 03  41.51093923 41.51093923 41.51093923 41.51093923
2   Wed Oct 04
3   Wed Oct 05  41.43764015 41.43764015 41.43764015
4   Wed Oct 06  41.21395681 41.21395681 41.21395681
5   Wed Oct 07  42.07607442 42.07607442 42.07607442
6   Wed Oct 08  42.0074109  42.0074109  42.0074109
7   Wed Oct 09  41.21395681 41.21395681
8   Wed Oct 10  41.43764015 41.43764015 41.43764015 41.43764015
9   Wed Oct 11  41.21395681 41.21395681 41.21395681 41.21395681
10  Wed Oct 12  41.43764015
11  Wed Oct 13
12  Wed Oct 14  42.07607442 42.07607442 42.07607442
13  Wed Oct 15  41.43764015 41.43764015 41.43764015 41.43764015
14  Wed Oct 16  41.21395681 41.21395681 41.21395681 41.21395681
15  Wed Oct 17
16  Wed Oct 18  42.07607442 42.07607442 42.07607442
Alderven
  • 7,569
  • 5
  • 26
  • 38