0

常に更新されるマスター ファイルと、毎分作成されるファイルがあります。毎分作成される新しいファイルを既存のマスター ファイルと比較できるようにしたいと考えています。これまでのところ、私は持っています:

with open("jobs") as a:
   new = a.readlines()

count=0
for item in new:
   new[count]=new[count].split(",")
   count+=1

これにより、マスター ファイルの各行の最初の index([0] を比較できるようになります。この時点で、私は自分自身を混乱させ始めます。次の行に沿ったものになると推測しています。

counter=0
for item in new:
    if new[counter][0] not in master:
        end = open("end","a")
        end.write(str(new[counter]) + "\n")
        counter+=1
        end.close()
    else:
         REPLACE LINES THAT ALREADY EXIST IN MASTER FILE WITH NEW LINE

ID は、新しいファイルが入るたびに必ずしも同じ順序になるとは限りません。新しいファイルには、ある時点でマスター ファイルよりも多くのエントリが含まれる場合があります。

意味が分からなかったり、情報を見逃していたり​​した場合は、お知らせください。明確にするよう努めます。ありがとう。

4

2 に答える 2

1

csv私には問題のように聞こえます。

残念ながら、マスターファイル自体、アウトファイル、またはその両方を変更したい場合、あなたの質問からは明らかではありません。これは2番目の処理を行います(マスターファイルとアップデートファイルを両方ともcsv形式で受け取り、マージされたものをソートせずに出力ファイルに出力します)。これがあなたが望むものではない場合、またはカンマで区切られたデータを取得したが、上にフィールド名がない場合、必要に応じて変更するのは簡単です。

import csv
with open("master.csv") as m, open("update.csv") as u, open("out.csv", "w") as o:
    master = { line['ID']: line for line in csv.DictReader(m) }
    update = { line['ID']: line for line in csv.DictReader(u) }
    master.update(update)
    fields = csv.DictReader(open("master.csv")).fieldnames
    out = csv.DictWriter(o, fields)
    out.writeheader()
    out.writerows(master.values())

master.csv を次のように使用します。

ID,Name,Foo,Bar,Baz,Description
1000001,Name here:1,1001,1,description here
1000002,Name here:2,1002,2,description here
1000003,Name here:3,1003,3,description here
1000004,Name here:4,1004,4,description here
1000005,Name here:5,1005,5,description here
1000006,Name here:6,1006,6,description here
1000007,Name here:7,1007,7,description here
1000008,Name here:8,1008,8,description here
1000009,Name here:9,1009,9,description here

および update.csv など:

ID,Name,Foo,Bar,Baz,Description
1000003,UPDATED Name here:3,1003,3, UPDATED description here
1000010,NEW ITEM Name here:9,1009,9,NEW ITEM description here 

out.csv に出力します。

ID,Name,Foo,Bar,Baz,Description
1000010,NEW ITEM Name here:9,1009,9,NEW ITEM description here ,
1000008,Name here:8,1008,8,description here,
1000009,Name here:9,1009,9,description here,
1000006,Name here:6,1006,6,description here,
1000007,Name here:7,1007,7,description here,
1000004,Name here:4,1004,4,description here,
1000005,Name here:5,1005,5,description here,
1000002,Name here:2,1002,2,description here,
1000003,UPDATED Name here:3,1003,3, UPDATED description here,
1000001,Name here:1,1001,1,description here,

順序は保持されないことに注意してください(必要に応じて質問から明確ではありません)。しかし、それは速くてきれいです。

于 2012-04-25T17:11:36.743 に答える
0

たぶん、このようなものがうまくいくでしょう:

#First create a set of all the ids contained in a masterfile
master_set = set()
with open('masterfile.txt') as mf:

    for ele in mf:
        master_set.add(ele.split(',')[0])

#if id is not in masterfile (or set) append the line to masterfile
with open('tempfile.txt') as temp, open('masterfile.txt', 'a') as mf:
    for line in temp:
        index = line.split(',')[0]
        if not index in master_set:
            master_set.add(index)
            mf.write(line)

私はそれをテストしていません。

于 2012-04-25T16:19:16.787 に答える