合并两个表(CSV)if(table1列A == table2列A) [英] Merge two tables (CSV) if (table1 column A == table2 column A)

查看:170
本文介绍了合并两个表(CSV)if(table1列A == table2列A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个CSV,可以在数字或Excel中打开,结构化:

|字| num1 |



|字| num2 |



如果两个字相等(就像它们都是hi和hi

|字| num1 | num2 |



这里有一些图片:




就像第1行一样,由于两个字都是相同的,TRUE,我想要它成为像

| TRUE | 5.371748 | 4.48957 |



通过一些小脚本,或者如果有一些功能/功能我可以忽略。 !

解决方案

使用dict:

  open('file1.csv','rb')as file_a,open('file2.csv','rb')as file_b:
data_a = csv.reader(file_a)
data_b = dict(csv.reader(file_b))#< - dict
with open('out.csv','wb')as file_out:
csv_out = csv.writer(file_out)
for word,num_a in data_a:
csv_out.writerow([word,num_a,data_b.get(word,'')])#< - edit

(未测试)


I have two CSV's, openable in Numbers or Excel, structured:
| word | num1 |
and
| word | num2 |

if the two words are equal (like they're both 'hi' and 'hi') I want it to become:
| word | num1 | num2 |

here are some pictures:

So like for row 1, since both the words are the same, 'TRUE', I want it to become something like
| TRUE | 5.371748 | 4.48957 |

Either through some small script, or if there's some feature/ function I'm overlooking.
Thanks!

解决方案

Use a dict:

with open('file1.csv', 'rb') as file_a, open('file2.csv', 'rb') as file_b:
    data_a = csv.reader(file_a)
    data_b = dict(csv.reader(file_b))  # <-- dict
    with open('out.csv', 'wb') as file_out:
        csv_out = csv.writer(file_out)
        for word, num_a in data_a:
            csv_out.writerow([word, num_a, data_b.get(word, '')])  # <-- edit

(untested)

这篇关于合并两个表(CSV)if(table1列A == table2列A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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