如何合并两个csv文件? [英] How to merge two csv files?

查看:310
本文介绍了如何合并两个csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的csv文件

I have two csv files like this

"id","h1","h2","h3", ...
"1","blah","blahla"
"4","bleh","bleah"

我想合并两个文件,以便如果两个文件中的ID相同,则该行的值应来自第二个文件。如果它们具有不同的ID,则合并的文件应同时包含这两行。

I'd like to merge the two files so that if there's the same id in both files, the values of the row should come from the second file. If they have different ids, then the merged file should contain both rows.

某些值有逗号

"54","34,2,3","blah"


推荐答案

res = {}

a=open('a.csv')
for line in a:
    (id, rest) = line.split(',', 1)
    res[id] = rest
a.close()

b=open('b.csv')
for line in b:
    (id, rest) = line.split(',', 1)
    res[id] = rest
b.close()

c=open('c.csv', 'w')
for id, rest in res.items():
    f.write(id+","+rest)
f.close()

基本上,您将每行的第一列用作字典 res 的键。因为b.csv是第二个文件,所以第一个文件(a.csv)中已经存在的密钥将被覆盖。最后,将 rest 再次合并到输出文件c.csv中。

Basically you're using the first column of each line as key in the dictionary res. Because b.csv is the second file, keys that already existed in the first file (a.csv) will be overwritten. Finally you merge key and rest together again in the output file c.csv.

标题行也将从第二个文件中获取,但是我想这些应该没有区别。

Also the header row will be taken from the second file, but these should not differ anyway I guess.

编辑:一种稍微不同的解决方案,可以合并任意数量的文件并按顺序输出行:

A slightly different solution that merges an arbitrary number of files and outputs rows in order:

res = {}
files_to_merge = ['a.csv', 'b.csv']
for filename in files_to_merge:
    f=open(filename)
    for line in f:
        (id, rest) = line.split(',', 1)
        if rest[-1] != '\n': #last line may be missing a newline
            rest = rest + '\n'
        res[id] = rest
    f.close()

f=open('c.csv', 'w')
f.write("\"id\","+res["\"id\""])
del res["\"id\""]
for id, rest in sorted(res.iteritems()):
    f.write(id+","+rest)
f.close()

这篇关于如何合并两个csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆