Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python以csv文件中的特定列为目标_Python - Fatal编程技术网

使用python以csv文件中的特定列为目标

使用python以csv文件中的特定列为目标,python,Python,我有以下python代码。我想比较两个csv文件,但是我只想比较第二个csv文件中的两列 我如何扩展下面的代码以只针对ProductSeld文件中的“ID”和“Name”列 import ctypes # An included library with Python install. def Mbox(title, text, style): return ctypes.windll.user32.MessageBoxW(0, text, title, style) try:

我有以下python代码。我想比较两个csv文件,但是我只想比较第二个csv文件中的两列

我如何扩展下面的代码以只针对ProductSeld文件中的“ID”和“Name”列

import ctypes  # An included library with Python install.
def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

try:
    with open('pipeNew2.csv', 'r') as t1, open('productsOld.csv', 'r') as t2:
        fileone = t1.readlines()
        filetwo = t2.readlines()



    with open('update.csv', 'w') as outFile:
        for line in filetwo:
            if line not in fileone:
                outFile.write(line)
    Mbox('Complete', 'Compairson Complete', 0)
except (ValueError,IOError) as err:
        Mbox('Error',(str(err)),0)
我试过的只是打印出所有的文件,就在同一行

import ctypes  # An included library with Python install.
import csv
def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

try:
    with open('pipeNew2.csv', 'r') as t1, open('productsOld.csv', 'r') as t2:
        fileone = t1.readlines()
        filetwo = t2.readlines()



    with open('update.csv', 'w') as outFile:
        newFile = csv.DictReader(filetwo)
        for row in newFile:
            if row['id'] not in fileone:
                outFile.write(str(row))
    Mbox('Complete', 'Compairson Complete', 0)
except (ValueError,IOError) as err:
        Mbox('Error',(str(err)),0)

查看内置模块

例如:

import ctypes  # An included library with Python install.
import csv


def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)


try:
    f1n = 'pipeNew2.csv'
    fn1 = ['catalogid', 'id', 'name']

    f2n = 'productsOld.csv'
    fn2 = ['id', 'name']

    with open(f1n, 'r', newline='') as t1, open(f2n, 'r', newline='') as t2:
        f1 = [line['id'] + line['name'] for line in csv.DictReader(t1, delimiter=',', fieldnames=fn1)]
        f2 = csv.DictReader(t2, delimiter=',', fieldnames=fn2)

        with open('update.csv', 'w+', newline='') as outFile:
            writer = csv.DictWriter(outFile, fieldnames=fn2)
            writer.writeheader()  # if csv have header

            for oldRow in f2:
                if not oldRow['id'] + oldRow['name'] in f1:
                    writer.writerow(oldRow)

except (ValueError, IOError) as err:
    Mbox('Error', (str(err)), 0)
ProductSeld.csv

id,name
1,aa
2,bb
3,cc
4,dd
pipeNew2.csv

catalogid,id,name
a,1,aa
b,2,bb
c,4,dd
update.csv

id,name
3,cc

好的,因此,csv模块帮助您解决问题中的“定位特定列”部分-如果您通过csv读卡器之一运行
fileone
file2
,它将为您解决列分离问题。但是,这仍然留下了需要决定行是否匹配的部分。你在这里期待什么?您是在比较相同位置的行,还是某个文件或其他文件中缺少某些行,或者这些行的顺序可能不同?可能更容易看到一些示例。我已经更新了OP。因此第一张图片是pipeNew文件和其他已售出的产品。正如您所看到的,它们的顺序大致相同,但行可能会丢失,ProductSald作为一个额外的列以及名称中都有标点符号,而pipeNew没有标点符号。我所希望的就是能够运行该程序,并让它用ProductSeld拥有的产品更新一个新文件,而pipeNew没有。您可以将两个csv文件读入两个数据帧,对它们进行操作并进行回写。对于循环,它比纯
要快得多。