0

I have two .csv files: inputfile.csv and mainfile.csv

I need to write a script that:

1- will read one by one all the records in inputfile.csv

2- then find if there is a match in the mainfile.csv

3- if there is a match then do nothing and read the next record from inputfile.csv

4- else if there is not a match in the mainfile.csv write that record from the inputfile.csv to the mainfile.csv

4

2 回答 2

1

此解决方案用于Scripting.Dictionary记录mainfile.csv. 然后,要查看 in 中的一行inputfile.csv是否是新行,只需查看该行是否存在于字典中。例如:

主文件.csv

exists,one
exists,two
exists,three
exists,four
exists,five

输入文件.csv

exists,two
new,one
exists,four
new,two
new,three

mainfile.csv (运行程序后)

exists,one
exists,two
exists,three
exists,four
exists,five
new,one
new,two
new,three

这是代码:


Option Explicit

Const ForReading = 1, ForWriting = 4, ForAppending = 8

Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oDict : Set oDict = CreateObject("Scripting.Dictionary")

'
' Read the contents of 'mainfile.csv'. Add each line to a dictionary
' to allow for a quick lookup.
'
Dim oFileMain : Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForReading)
Dim sLine
While Not oFileMain.AtEndOfStream
    sLine = oFileMain.ReadLine()
    oDict.Add sLine, True
Wend
oFileMain.Close
Set oFileMain = Nothing

'
' Re-open 'mainfile.csv' in append mode.
'
Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForAppending)

'
' Read the contents of 'inputfile.csv'. Write a line to 'mainfile.csv'
' only if that line does not exist in the dictionary.
'
Dim oFileInput : Set oFileInput = oFso.OpenTextFile("inputfile.csv", ForReading)
While Not oFileInput.AtEndOfStream
    sLine = oFileInput.ReadLine()
    If Not oDict.Exists(sLine) Then  ' not a duplicate!
        WScript.Echo "Found new line: [" & sLine & "]"
        oFileMain.WriteLine sLine
    End If
Wend
oFileInput.Close
Set oFileInput = Nothing

'
' Wrap it up.
'
oFileMain.Close
Set oFileMain = Nothing

Set oDict = Nothing
Set oFso = Nothing

' End
于 2012-09-03T02:30:18.343 回答
0

这是我在 python 中进行的最佳尝试,不知道文件的结构:

with open("mainfile.csv", "r") as main:
    records = [x.strip() for x in main.readlines()]
with open("inputfile.csv", "r") as input:
    inputs = [x.strip() for x in input.readlines()]
for input in inputs:
    if input not in records:
        records.append(input)
with open("mainfile.csv", "w") as main:
    for record in records:
        main.write(record + "\n")

因此,对于以下文件,您可以从以下内容开始:

输入文件.csv:

A quick brown fix
Two turtle doves
Feather boa

主文件.csv:

Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing

运行脚本后 mainfile.csv 如下所示:

Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing
A quick brown fix
于 2012-08-30T21:28:57.737 回答