在Python中将行从一种CSV追加到另一种

时间:2019-02-18 14:48:37

标签: python pandas csv merge dataset

我已经看过很多解决方案,但是找不到适合我想要做的事情。

基本上我有2个CSV文件:

  

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 

我想将original.csv附加到all.csv,方法是简单地将original.csv中的所有行合并到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
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 

如您所见,数据没有标题,并且行的长度不同。这只是我正在使用的文件类型的示例,但是我想获得一种可以在任何CSV上使用的解决方案。

我正在使用Python3,到目前为止,我尝试使用pandas库,但没有运气。

任何建议都会很棒,谢谢。

1 个答案:

答案 0 :(得分:2)

您不需要使用pandas。只需将一个csv附加到另一个csv:

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

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

输出:

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