基于其他文件数据的CSV文件

时间:2015-03-12 17:48:15

标签: python file csv import-from-csv

我想用Python做到这一点。

给出两个输入CSV文件(data1.csv)和(data2.csv) 生成一个遵循以下规范的新文件(sample.csv):

  • data1.csv在文件的第一行包含标题
  • data2.csv在文件的第一行包含标题
  • data1.csv的列数多于data2.csv

  • sample.csv应该包含data1.csv中的所有行,但只包含那些列 出现在data2.csv中,并按照它们出现在data2.csv

  • 中的顺序出现

示例:

 data1.csv
 h1,h2,h3,h4,h5,h6
 0,1,2,3,4,5
1,2,3,4,5,6

data2.csv
h1,h4,h3,h6
0,1,2,3

sample.csv
h1,h4,h3,h6
0,3,2,5
1,4,3,6

 My code:

   import csv
    import array
   import os

  with open('C:\\Users\\nithin\\Desktop\\data1.csv') as f:
     r = csv.reader(f, delimiter=',')
    dict1 = {row[0]: row[1:] for row in r if (row[0]=='h1')}

   print str(dict1)

1 个答案:

答案 0 :(得分:0)

这使得工作:

import csv
with open('/tmp/data2.csv') as f:
    headerList = f.readline().split(",")
with open('/tmp/data1.csv') as f:
    r = csv.DictReader(f, delimiter=',')
    with open('/tmp/sample.csv', 'w') as fout:
        fout.write(','.join(headerList))
        for row in r:
            lineTab = [row[head.rstrip()] for head in headerList]
            fout.write(",".join(lineTab)+"\n")