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ます。次に、行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 に答える