Python CSV将单元格从一个csv文件复制到另一个文件

时间:2015-01-29 11:59:37

标签: python csv

编辑:这已经解决了。我已将解决方案添加到此帖的底部。

我有两个文件,newStock.csv和sortedBlad.csv。

newStock.csv看起来像

SKU_IN,QTY_IN
3201MO-1,1
320205-1,4

sortedBlad.csv看起来像

eid,sku,asd,asdf,Asdg,QTY,sdfasf,asdf,sdfsf,dfsdf,sdfs,dddd,dssss,Name,dddddpo,odommm,mdmdjkon,kkkkknn,okokokok,okokokb,jj,mb,bgtio,

1.11E+11,3201MO-1,,,,5,55,,£20.90,08-Aug-14,,New,9,FORD FOCUS MK1 98 04  OIL SUMP PAN FOR ENGINE  1.8 16V 2.0 16V  & ST RS PETROL,Oil Sumps,174039,,3,27-Jan-15 11:44:30 GMT,,,1000,FALSE,

我目前的代码通过sortedBlad.csv并复制eid,sku和name列并将它们插入到另一个文件中。它还匹配来自newStock的skus并将它们附加到每行的末尾。我面临的问题是,我似乎无法从新股票获得数量,也可以附加到每一行的末尾。目前代码的输出如下。

    1.11E+11,3201MO-1,5,FORD FOCUS MK1 98 04  OIL SUMP PAN FOR ENGINE  1.8 16V 2.0 16V  & ST RS PETROL,3201MO-1,,
    1.21E+11,320205-1,6,FORD FOCUS 2004  2008 FRONT BUMPER  RADIATOR GRILLE BONNET WITH CHROME TRIM,320205-1,,

您可以看到sku被附加到行的末尾。我还想在sku之后添加newStock数量。

我到目前为止的代码如下。

import csv

test_file = "sortedBlad.csv"
test_file2 = "sortedNewStock.csv" 

test_file2_array = []

finalFile = "final.csv"

csv_f_array = []

csv_file = csv.DictReader(open(test_file, 'r'), delimiter=',', quotechar='"')
csv_file2 = csv.DictReader(open(test_file2, 'r'), delimiter=',', quotechar='"')


with open(test_file2, 'rt') as a:
        reader = csv.reader(a, delimiter=",")
        for row in reader:
            test_file2_array.append(row[0])

#print(test_file2_array)

file = open(finalFile, 'w')

for eachline in csv_file:
    for SKU_IN in test_file2_array:
        if SKU_IN == eachline['sku']:
            print(SKU_IN+'-'+test_file2_array[1])
            # why wont you write the qty?
         file.write(eachline['eid']+','+eachline['sku']+','+eachline['QTY']+','+eachline['Name']+','+SKU_IN+','+',\n')

file.close

感谢回复人员。我设法通过在reddit的learnpython中的learnpython irc的某些人的帮助解决了这个问题。

解决方案是使用字典将SKU_IN存储为密钥,并将QTY_IN作为值。然后在我写文件时调用它。

3 个答案:

答案 0 :(得分:1)

您是否考虑过使用与表格数据集本身一致的软件包来支持您想要的操作 - 例如pandas

import pandas as pd

f1=pd.read_csv("sortedBlad.csv")
f2=pd.read_csv("sortedNewStock.csv")

#Filter columns in f1
f1sub=f1[ ['eid', 'sku', 'name' ] ]

#Merge data
mergef = pd.merge(f1sub,f2,left_on='sku',right_on='SKU_IN')

合并功能会自动为您处理对齐。

答案 1 :(得分:0)

您只是将sortedNewStock.csvSKU_IN)的第一列追加到test_file2_array

test_file2_array.append(row[0])

您还需要从该文件中获取QTY_IN列。我建议以下内容添加SKU_INQTY_IN

test_file2_array.append(row[0:2])

然后改变

for SKU_IN in test_file2_array:

for SKU_IN, QTY_IN in test_file2_array:

所以你的代码看起来像这样:

.
.
.
with open(test_file2, 'rt') as a:
    test_file2_array = [row[0:2] for row in csv.reader(a, delimiter=",")]

#print(test_file2_array)

file = open(finalFile, 'w')

for eachline in csv_file:
    for SKU_IN, QTY_IN in test_file2_array:
        if SKU_IN == eachline['sku']:
            print(SKU_IN + '-' + QTY_IN)
            file.write(','.join((eachline['eid'], eachline['sku'],
                                 eachline['QTY'], eachline['Name'],
                                 SKU_IN, QTY_IN, '\n')))

答案 2 :(得分:0)

您没有将QTY_IN信息从newStock.csv保留在任何地方。

import csv

blad_file = "blad.csv"
newStock_file = "stock.csv" 

newStock_file_array = []

finalFile = "final.csv"

csv_blad = csv.DictReader(open(blad_file, 'r'), delimiter=',', quotechar='"')
csv_newStock = csv.DictReader(open(newStock_file, 'r'), delimiter=',', quotechar='"')

for line in csv_newStock:
    newStock_file_array.append(line)

with open(finalFile, 'w') as file_out:
    for eachline in csv_blad:
        for newStock in newStock_file_array:
            if newStock['SKU_IN'] == eachline['sku']:
                # print(newStock['SKU_IN']+'-'+newStock['QTY_IN'])
                file_out.write(eachline['eid']+','+eachline['sku']+','+eachline['QTY']+','+eachline['Name']+','+newStock['QTY_IN']+','+'\n')